#include #define FOR(i,a,b) for(int i = (a); i < (b); ++i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for(int i = (n) - 1; (i) >= 0; --i) #define SZ(n) (int)(n).size() #define ALL(n) (n).begin(), (n).end() #define MOD LL(1e9 + 7) #define INF 100000000 using namespace std; typedef long long LL; typedef vector VI; typedef pair PI; int d[20][20]; int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; int main() { int w, h; cin >> w >> h; char c[20][20]; int sx, sy; REP(i, h) { REP(j, w) { d[i][j] = INF; } } REP(i, h) { REP(j, w) { cin >> c[i][j]; if (c[i][j] == '.') { sy = i; sx = j; } } } d[sy][sx] = 0; queue q; q.push(make_pair(sy, sx)); while (!q.empty()) { PI t = q.front(); q.pop(); REP(i, 4) { int ny = t.first + dy[i], nx = t.second + dx[i]; if (ny >= 0 && nx >= 0 && ny < h && nx < w) { int a = (c[ny][nx] == '#'); if (d[t.first][t.second] + a < d[ny][nx]) { d[ny][nx] = d[t.first][t.second] + a; q.push(make_pair(ny, nx)); } } } } int ans = 0; REP(i, h) { REP(j, w) { if (c[i][j] == '.') ans = max(ans, d[i][j]); } } cout << ans << endl; return 0; }