#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) using namespace std; template auto vectors(T a, X x) { return vector(x, a); } template auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector(x, cont); } int main() { // input int h, w; scanf("%d%d", &h, &w); vector > is_sea = vectors(false, h+2, w+2); repeat_from (y,1,h+1) { repeat_from (x,1,w+1) { char c; scanf(" %c", &c); is_sea[y][x] = c == '#'; } } // compute vector > dist = vectors(-1, h+2, w+2); queue > que; repeat (y,h+2) { repeat (x,w+2) { if (not is_sea[y][x]) { dist[y][x] = 0; que.emplace(y, x); } } } int ans = -1; while (not que.empty()) { int y, x; tie(y, x) = que.front(); que.pop(); ans = dist[y][x]; for (int dy : { -1, 0, 1 }) { for (int dx : { -1, 0, 1 }) { int ny = y + dy; int nx = x + dx; if (ny < 0 or h+2 <= ny or nx < 0 or w+2 <= nx) continue; if (dist[ny][nx] == -1) { dist[ny][nx] = dist[y][x] + 1; que.emplace(ny, nx); } } } } // output printf("%d\n", ans); return 0; }