#include #include using namespace std; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x dr = {0, 0, 1, -1}, dc = {1, -1, 0, 0}; int w, h; vector kort; void dfs(int r, int c, vector &holes) { kort[r][c] = '#'; holes.push_back(Point(r, c)); for(int i : range(dr.size())) { int nr = r + dr[i]; int nc = c + dc[i]; if(nr < 0 || h <= nr || nc < 0 || w <= nc) { continue; } if(kort[nr][nc] == '.') { dfs(nr, nc, holes); } } } Point find_hole() { int pr, pc; for(int r : range(h)) { for(int c : range(w)) { if(kort[r][c] == '.') { pr = r, pc = c; break; } } } return Point(pr, pc); } const int inf = 987654321; int main(void) { scanf("%d%d", &w, &h); kort.assign(h, string()); vector holes1, holes2; for(int i : range(h)) { cin >> kort[i]; } Point h1 = find_hole(); dfs(h1.r, h1.c, holes1); Point h2 = find_hole(); dfs(h2.r, h2.c, holes2); int lag = inf; for(Point p1 : holes1) { for(Point p2 : holes2) { int dist = p1.dist(p2) - 1; lag = min(lag, dist); } } printf("%d\n", lag); return 0; }