#include #include #include #include using namespace std; int W, H; int dist[3010][3010] = { 0 }; char s[3010]; int main(void) { cin >> H >> W; queue Q; for (int i = 2; i <= H + 1; i++) { scanf("%s", s); for (int j = 2; j <= W + 1; j++) { if (s[j - 2] == '#') { dist[i][j] = 1510; }else { Q.push(0); Q.push(j); Q.push(i); } } } for (int i = 2; i <= H + 1; i++) { Q.push(0); Q.push(1); Q.push(i); Q.push(0); Q.push(W + 2); Q.push(i); } for (int i = 2; i <= W + 1; i++) { Q.push(0); Q.push(i); Q.push(1); Q.push(0); Q.push(i); Q.push(H + 2); } int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 }; int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 }; int t, x, y, nx, ny; while (Q.size()) { t = Q.front(); Q.pop(); x = Q.front(); Q.pop(); y = Q.front(); Q.pop(); for (int i = 0; i < 8; i++) { nx = x + dx[i]; ny = y + dy[i]; if (dist[ny][nx] > t + 1) { dist[ny][nx] = t + 1; Q.push(t + 1); Q.push(nx); Q.push(ny); } } } cout << dist[y][x] << endl; return 0; }