#include #include #include #include #include #include using namespace std; #define rep(i,a,b) for(int i=a;i> w >> h; char c[h][w]; rep(i,0,h) rep(j,0,w) cin >> c[i][j]; vector< vector> > arr; rep(i,0,h) rep(j,0,w){ if(c[i][j] == '.'){ vector> tmp; vector> stk; stk.push_back(make_pair(i,j)); while(!stk.empty()){ pair t = stk.back(); stk.pop_back(); c[t.first][t.second] = '#'; tmp.push_back(make_pair(t.first,t.second)); rep(k,0,4){ int y = t.first + dy[k]; int x = t.second + dx[k]; if(-1 < y && y < h && -1 < x && x < w && c[y][x] == '.'){ stk.push_back(make_pair(y,x)); } } } arr.push_back(tmp); } } int ans = inf; for(auto a: arr[0]){ for(auto b: arr[1]){ ans = min(ans,abs(a.first - b.first) + abs(a.second - b.second)); } } cout << ans-1 << endl; }