Problem:
Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i = 0; i < n; i++)
#define PER(i,n) for(int i = n-1; i >= 0; i--)
#define FOR(i,l,r) for(int i = l; i <= r; i++)
#define ROF(i,l,r) for(int i = r; i >= l; i--)
#define DEBUG(x) cout << #x << "=" << x << endl;
#define SHOW1(A,n) { REP(i,n) cout<<A[i]<<(i==n-1?'\n':' '); }
#define SHOW2(A,m,n) { REP(j,m) SHOW1(A[j],n) }
#define pb push_back
#define fi first
#define se second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)((x).size())
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int INF = 0x3f3f3f3f, MOD = 998244353;
const double PI = acos(-1), EPS = 1e-15;
const int MAXN = 1e3+9, MAXM = 2e5+9;

int m,n,vis[MAXN][MAXN];
char s[MAXN][MAXN];
const int r[]={0,0,1,-1};
const int c[]={1,-1,0,0};
#define vld(i,j) (0<=i&&i<m&&0<=j&&j<n)
struct node { int x,y,t; };
int bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node> Q;
    REP(i,m)REP(j,n) if (s[i][j]=='#') Q.push({i,j,0}), vis[i][j]=1;
    int ans=0;
    while (!Q.empty()) {
        node now=Q.front(); Q.pop();
        int x=now.x,y=now.y,t=now.t;
        ans=max(ans,t);
        REP(i,4) {
            int xx=x+r[i], yy=y+c[i];
            if (vld(xx,yy) && !vis[xx][yy]) {
                vis[xx][yy]=1;
                Q.push({xx,yy,t+1});
            }
        }
    }
    return ans;
}

int main()
{
#ifdef LOCAL
    //freopen("i.txt", "r", stdin);
    //freopen("o.txt", "w", stdout);
#endif //LOCAL

    while (cin>>m>>n) {
        REP(i,m) scanf("%s", s[i]);
        cout<<bfs()<<endl;
    }

    return 0;
}