#include #include #include #include using namespace std; bool tof,tof2; int h, w; pair nx; int solve(int cnt, vector > &land, pair nx) { tof2 = false; int mn, mx; for (int i = nx.first; i < nx.second; i++) { for (int j = 1; j < w + 1; j++) { if (land[i][j] == cnt) { tof = false; for (int a = -1; a < 2; a++) { for (int b = -1; b < 2; b++) { if (land[i + a][j + b] < cnt) { tof = true; break; } } if (tof) break; } if (!tof) { land[i][j]++; if (!tof2) mn = i; mx = i; tof2 = true; } } } } if (!tof2) return cnt; nx = make_pair(mn, mx); cnt = solve(++cnt, land ,nx); return cnt; } int main() { cin >> h >> w; vector > land; land.resize(h + 2); for (int i = 0; i < h + 2; i++) { if (i == 0 || i == h + 1) land[i].resize(w+2); else { land[i].resize(w + 2); } } for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { char tmp; cin >> tmp; if (tmp == '.') land[i][j] = 0; else if (tmp == '#') land[i][j] = 1; } } int cnt = 1; nx = make_pair(1, h + 1); cout << solve(cnt, land,nx) << endl; return 0; }