#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int main() { int w, h; cin >> w >> h; string s[h]; rep(i,h)cin >> s[i]; vector

u,v; int num = 0; rep(i,h){ rep(j,w){ if(s[i][j] == '.'){ queue

que; que.push(P(i,j)); if(num == 0) u.push_back(P(i,j)); else v.push_back(P(i,j)); s[i][j] = '#'; while(que.size()>0){ int x = que.front().first; int y = que.front().second; que.pop(); rep(k,4){ int nx = x + dx[k]; int ny = y + dy[k]; if(s[nx][ny] == '.'){ if(num == 0) u.push_back(P(nx,ny)); else v.push_back(P(nx,ny)); que.push(P(nx,ny)); } s[nx][ny] = '#'; } } num++; } } } int ans = 1001001; rep(i,u.size()){ rep(j,v.size()){ int tmp = abs(u[i].first - v[j].first) + abs(u[i].second - v[j].second); ans = min(ans, tmp); } } cout << ans << endl; return 0; }