#include #include #include #include #include #include #include #include using namespace std; typedef tuple TUP; const vector dx = {1, 1, 0, -1, -1, -1, 0, 1}; const vector dy = {0, 1, 1, 1, 0, -1, -1, -1}; const int INF = 1e9; void show(vector> &v) { cout <<"---------" << endl; for (auto vv : v) { for (auto vvv : vv) { if (vvv == INF) { printf("- "); } else { printf("%d ", vvv); } } printf("\n"); } } int main() { int h, w; cin >> h >> w; vector s(h); for (int i = 0; i < h; i++) { cin >> s[i]; } vector> t(h, vector(w,INF)); queue que; for (int i = 0; i < h; i++) { que.push(make_tuple(-1, i, 0)); que.push(make_tuple( w, i, 0)); } for (int i = 0; i < w; i++) { que.push(make_tuple(i, -1, 0)); que.push(make_tuple(i, h, 0)); } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '.') { que.push(make_tuple(j, i, 0)); t[i][j] = 0; } } } int ans = 0; while (!que.empty()) { auto tup = que.front(); que.pop(); int x = get<0>(tup); int y = get<1>(tup); int v = get<2>(tup); int nv = v + 1; for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) { continue; } if (nv < t[ny][nx]) { t[ny][nx] = nv; que.push(make_tuple(nx, ny, nv)); ans = max(ans, nv); } } } // show(t); cout << ans << endl; return 0; }