#include #include #include #include #include #include #include #include using namespace std; #define GET_ARG(a,b,c,F,...) F #define REP3(i,s,e) for (i = s; i <= e; i++) #define REP2(i,n) REP3 (i,0,(int)(n)-1) #define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__) #define RREP3(i,s,e) for (i = s; i >= e; i--) #define RREP2(i,n) RREP3 (i,(int)(n)-1,0) #define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__) #define DEBUG(x) cerr << #x ": " << x << endl typedef long long ll; constexpr int INF = 1e8; constexpr int MOD = 1e9+7; constexpr int ESP = 1e-9; constexpr double PI = acos(-1); string s[3000]; int dist[3000][3000]; bool used[3000][3000]; int dxy[] = {0,1,-1,-1,0,-1,1,1}; int h, w; int main(void) { int i, j; cin >> h >> w; REP (i,h) cin >> s[i]; REP (i,h) REP (j,w) dist[i][j] = INF; queue>> q; REP (i,h) REP (j,w) if (s[i][j] == '.') { q.push(make_pair(0,make_pair(i,j))); dist[i][j] = 0; } REP (i,h) REP (j,w) { if (s[i][j] == '#' && (i == 0 || i == h-1 || j == 0 || j == w-1)) { q.push(make_pair(1,make_pair(i,j))); dist[i][j] = 1; } } while (!q.empty()) { int d = q.front().first; int y = q.front().second.first; int x = q.front().second.second; q.pop(); REP (i,8) { int ny = y + dxy[i]; int nx = x + dxy[(i+1)%8]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (dist[ny][nx] > d + 1) { q.push(make_pair(d+1,make_pair(ny,nx))); dist[ny][nx] = d + 1; } } } int ans = 0; REP (i,h) REP (j,w) ans = max(ans,dist[i][j]); cout << ans << endl; return 0; }