#include "bits/stdc++.h" using namespace std; bool check[30][30]; int dist[30][30]; int dy[] = {1, 0, -1, 0}; int dx[] = { 0, 1, 0, -1 }; int W, H; bool ok(int y, int x){ return y >= 0 && x >= 0 && y < H && x < W; } int main() { cin >> W >> H; vector board(H); for (int i = 0; i < H; i++) { cin >> board[i]; } queue> q; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (board[i][j] == '.'){ check[i][j] = true; q.push(make_pair(i, j)); break; } } if (!q.empty()) break; } while (!q.empty()){ int y = q.front().first; int x = q.front().second; q.pop(); for (int k = 0; k < 4; k++) { int ny = y + dy[k]; int nx = x + dx[k]; if (!ok(ny, nx)) continue; if (check[ny][nx]) continue; if (board[ny][nx] != '.') continue; check[ny][nx] = true; q.push(make_pair(ny, nx)); } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (board[i][j] == '.' && !check[i][j]){ check[i][j] = true; q.push(make_pair(i, j)); dist[i][j] = 0; } else dist[i][j] = 999; } } while (!q.empty()){ int y = q.front().first; int x = q.front().second; q.pop(); for (int k = 0; k < 4; k++) { int ny = y + dy[k]; int nx = x + dx[k]; if (!ok(ny, nx)) continue; if (dist[ny][nx] < 999) continue; if (board[ny][nx] == '.'){ cout << dist[y][x] << endl; return 0; } check[ny][nx]; q.push(make_pair(ny, nx)); dist[ny][nx] = dist[y][x] + 1; } } cout << "error" << endl; }