#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define Getsign(n) ((n > 0) - (n < 0)) typedef vector Ivec; typedef pair Pos; const pair dir[8] = { {0,1},{1,1}, {1,0},{0,-1},{-1,-1},{1,-1},{-1,1}, {-1,0} }; #define MAX(a,b) (((a)>(b))?(a):(b)) #define Chebi(a,b) (MAX(abs(a.first-b.first),abs(a.second-b.second))) int main() { int h, w; scanf("%d %d%*c", &h, &w); int map[3004][30004] = {}; Pos poss[3004][3004]; queue wf; for (int i = 2; h + 1 >= i; i++) { for (int j = 2; w + 1 >= j; j++) { char c; scanf("%c", &c); map[i][j] = c == '#' ? 1 : 0; if (c == '.') { wf.push({ i,j }); poss[i][j] = { i,j }; } } scanf("%*c"); } for (int i = 1; h + 2 >= i; i++) { wf.push({ 1, i }); wf.push({ h + 2, i }); poss[1][i] = { 1, i }; poss[h + 2][i] = {h+2, i}; } for (int i = 1; w + 2 >= i; i++) { wf.push({ i, 1 }); wf.push({i,w+2 }); poss[i][1] = { i,1}; poss[i][w+2] = {i, w + 2}; } while (!wf.empty()) { Pos sea = wf.front(); wf.pop(); for (int i = 0; 8 > i; i++) { Pos tar = { sea.first + dir[i].first ,sea.second + dir[i].second }; if (map[tar.first][tar.second] == 1) { if (poss[tar.first][tar.second].first == 0 || (Chebi(tar, poss[sea.first][sea.second]) < Chebi(tar, poss[tar.first][tar.second]))) { if (poss[tar.first][tar.second].first == 0 || poss[tar.first][tar.second].first != poss[sea.first][sea.second].first || poss[tar.first][tar.second].second != poss[sea.first][sea.second].second) { poss[tar.first][tar.second] = poss[sea.first][sea.second]; wf.push(tar); } } } } } int maxim = -1; for (int i = 2; h + 1 >= i; i++) { for (int j = 2; w + 1 >= j; j++) { if (map[i][j] != 1)continue; Pos tar = { i,j }; maxim = max(maxim, Chebi(poss[i][j], tar)); } } printf("%d", maxim); return 0; }