#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; int main() { int w, h; cin >> w >> h; vector c(h+2, string(w+2, '!')); int sy, sx; for(int y=1; y<=h; ++y){ for(int x=1; x<=w; ++x){ cin >> c[y][x]; if(c[y][x] == '.'){ sy = y; sx = x; } } } queue > q1, q2; q1.push(make_pair(sy, sx)); q2.push(make_pair(sy, sx)); c[sy][sx] = '!'; while(!q1.empty()){ int y = q1.front().first; int x = q1.front().second; q1.pop(); for(int i=0; i<4; ++i){ int y2 = y + dy[i]; int x2 = x + dx[i]; if(c[y2][x2] == '.'){ q1.push(make_pair(y2, x2)); q2.push(make_pair(y2, x2)); c[y2][x2] = '!'; } } } int ret = 0; for(;;){ int n = q2.size(); while(--n >= 0){ int y = q2.front().first; int x = q2.front().second; q2.pop(); for(int i=0; i<4; ++i){ int y2 = y + dy[i]; int x2 = x + dx[i]; if(c[y2][x2] == '.'){ cout << ret << endl; return 0; } if(c[y2][x2] == '#'){ q2.push(make_pair(y2, x2)); c[y2][x2] = '!'; } } } ++ ret; } }