#include using namespace std; typedef pair P; string field[20]; int W, H; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; void dfs(int x, int y, vector

& p) { if (field[y][x] == '#') return; p.emplace_back(P(x, y)); field[y][x] = '#'; for (int i = 0; i < 4; i++) { int w = x + dx[i], h = y + dy[i]; if (w < 0 || w >= W || h < 0 || h >= H) continue; dfs(w, h, p); } return; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> W >> H; for (int i = 0; i < H; i++) cin >> field[i]; vector

a, b; bool flag = false; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (field[i][j] == '.') { dfs(j, i, a); flag = true; break; } } if (flag) break; } flag = false; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (field[i][j] == '.') { dfs(j, i, b); flag = true; break; } } if (flag) break; } int ans = 1000; for (unsigned int i = 0; i < a.size(); i++) { for (unsigned int j = 0; j < b.size(); j++) { int dis = abs(a[i].first - b[j].first) + abs(a[i].second - b[j].second) - 1; ans = min(ans, dis); } } cout << ans << "\n"; return 0; }