#include #include #include #include #include #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; queue> q; for (int i = 2; i < h + 2; i++) { q.push({ i, 1 }); q.push({ i, w + 2 }); } for (int j = 2; j < w + 2; j++) { q.push({ 1, j }); q.push({ h + 2, j }); } vector> a(h + 4, vector(w + 4, 0)); string s; for (int i = 0; i < h; i++) { cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') { a[i + 2][j + 2] = 1 << 14; } else { q.push({ i + 2, j + 2 }); } } } int r = 0; while (!q.empty()) { auto p = q.front(); q.pop(); int d = a[p.first][p.second] + 1; for (int i = p.first - 1; i <= p.first + 1; i++) { for (int j = p.second - 1; j <= p.second + 1; j++) { if (a[i][j] > d) { r = a[i][j] = d; q.push({ i, j }); } } } } cout << r << endl; return 0; }