#include #include #include #include #include #include #include #include #include #include #include using ll = long long; using namespace std; vector dx{1, 0, -1, 0}, dy{0, 1, 0, -1}; vector>> pos(2); void bfs1(vector &v, const int x, const int y, const int cnt){ int nx, ny; for (int i = 0; i < 4; i++){ nx = x + dx[i]; ny = y + dy[i]; if (nx >= 0 && nx < v[0].size() && ny >= 0 && ny < v.size() && (v[ny][nx] == '.')) { v[ny][nx] = '*'; pos[cnt].push_back(make_pair(ny, nx)); bfs1(v, nx, ny, cnt); } } } int main(){ int w, h, cnt = 0, min1 = 50; cin >> w >> h; vector v(h); for (int i = 0; i < h; i++) cin >> v[i]; int ast = 0; for (int i = 1; i < h - 1; i++) { for (int j = 1; j < w - 1; j++){ if (v[i][j] == '.'){ v[i][j] = '*'; pos[cnt].push_back(make_pair(i, j)); bfs1(v, j, i, cnt); cnt++; ast++; } } if (ast == 2) break; } for (auto x : pos[0]){ for (auto y : pos[1]){ min1 = min(min1, abs(y.first - x.first) + abs(y.second - x.second) -1); } } cout << min1 << endl; }