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: Wrong Answer)

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

using ll = long long;
const int INF = 1<<29;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
#ifdef LOCAL_ENV
	#define debug(var) std::cout<<#var" = "<<var<<std::endl
#else
	#define debug(var)
#endif
#define p(var)  std::cout<<var<<std::endl
#define PI (acos(-1))
#define rep(i, n)   for(int i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(int i=1, i##_length=(n); i<=i##_length; ++i)
#define all(a) (a).begin(), (a).end()
#define pb push_back
inline double isnatural(double a) { return a>=0 && ceil(a)==floor(a); }
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b,a%b) : a; }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a,  b) * b; }
template<typename T1, typename T2>inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2>inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
	for (int i = 0, len = v.size(); i < len; ++i){
		s << v[i]; if (i < len - 1) s << "\t";
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
	for (int i = 0, len = vv.size(); i < len; ++i){
		s << vv[i] << endl;
	}
	return s;
}
template<typename T1, typename T2> ostream& operator<<(ostream& s, const map<T1, T2>& m) {
	s << "{" << endl;
	for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr){
		s << "\t" << (*itr).first << " : " << (*itr).second << endl;
	}
	s << "}" << endl;
	return s;
}

/*-----8<-----8<-----*/

int H,W;
vector<vector<int>> dis;
vector<int> hh(2,-1),ww(2,-1);
void update(int h,int w){
	int t;
	{
		int i=0;
		int j=0;
		t=abs(i-h)+abs(j-w);
		chmin(dis[i][j],t);
		i=H-1;
		t=abs(i-h)+abs(j-w);
		chmin(dis[i][j],t);
	}
	{
		int i=0;
		int j=W-1;
		t=abs(i-h)+abs(j-w);
		chmin(dis[i][j],t);
		i=H-1;
		t=abs(i-h)+abs(j-w);
		chmin(dis[i][j],t);
	}

	
	debug(dis);
	rep(i,2){
		if(hh[i]==-1)continue;
		rep(j,2){
			if(ww[j]==-1)continue;
			t=abs(hh[i]-h)+abs(ww[j]-w);
			chmin(dis[hh[i]][ww[j]],t);
		}
	}
}
int main() {


	cin >> H >> W;
	
	
	if(H%2==0){
		hh[0]=H/2-1;
		hh[1]=H/2;
	}else{
		hh[0]=(H-1)/2;
	}
	if(W%2==0){
		ww[0]=W/2-1;
		ww[1]=W/2;
	}else{
		ww[0]=(W-1)/2;
	}

	vector<string> a(H,"");
	dis=vector<vector<int>>(H,vector<int>(W,INF));
	rep(i,H){
		cin >> a[i];
	}

	rep(i,H){
		rep(j,W){
			if(a[i][j]=='#')update(i,j);
		}
	}




	int maxval=0;
	{
		int i=0;
		int j=0;
		chmax(maxval,dis[i][j]);
		j=W-1;
		chmax(maxval,dis[i][j]);
	}
	{
		int i=H-1;
		int j=0;
		chmax(maxval,dis[i][j]);
		j=W-1;
		chmax(maxval,dis[i][j]);
	}
	rep(i,2){
		if(hh[i]==-1)continue;
		rep(j,2){
			if(ww[j]==-1)continue;
			chmax(maxval,dis[hh[i]][ww[j]]);
		}
	}

	p(maxval);
	return 0;
}