#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; using P = pair; constexpr ld EPS = 1e-12; constexpr int INF = numeric_limits::max() / 2; constexpr int MOD = 1e9 + 7; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int W, H; cin >> W >> H; vector s(H); int sx, sy; for (int i = 0; i < H; i++) { cin >> s[i]; for (int j = 0; j < W; j++) { if (s[i][j] == '.') { sx = i; sy = j; } } } vector> dis(H, vector(W, INF)); queue

q; q.push(P(sx, sy)); dis[sx][sy] = 0; while (!q.empty()) { P p = q.front(); q.pop(); int x = p.first, y = p.second; s[x][y] = '#'; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue; if (dis[nx][ny] != INF) continue; if (s[nx][ny] != '.') continue; dis[nx][ny] = 0; q.push(P(nx, ny)); } } for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (dis[i][j] == 0) q.push(P(i, j)); while (!q.empty()) { P p = q.front(); q.pop(); int x = p.first, y = p.second; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue; if (dis[nx][ny] != INF) continue; if (s[nx][ny] == '.') { cout << dis[x][y] << endl; return 0; } dis[nx][ny] = dis[x][y] + 1; q.push(P(nx, ny)); } } }