#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector VI; typedef vector VVI; #define REP(i, n) for(int(i)=0;(i)<(n);++(i)) #define FOR(i, f, t) for(int(i)=(f);(i)<(t);(++i)) #define RREP(i, n) for(int(i)=(n)-1;(i)>=0;--(i)) const int MOD = int(1e9+7); int N,M,W,H; int res = 0; int c[111111]; const int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1},{1,-1},{1,1},{-1,1},{-1,-1}}; int main(){ do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0); cin >> W >> H; vector v; REP(i,H){ string s; cin >> s; v.push_back(s); } int k = 0; vector> kk[2]; REP(y,H)REP(x,W){ if(v[y][x] == '.'){ deque > q; q.push_back(make_pair(x,y)); kk[k].push_back(make_pair(x,y)); v[y][x] = '#'; while(!q.empty()){ int xx = q.front().first; int yy = q.front().second; q.pop_front(); REP(i,4){ int mx = xx + dir[i][0]; int my = yy + dir[i][1]; if(mx < 0 || my < 0 || mx >= W || my >= H) continue; if(v[my][mx] == '#') continue; q.push_back(make_pair(mx,my)); kk[k].push_back(make_pair(mx,my)); v[my][mx] = '#'; } } k++; } } int minv = 99999; int s0 = kk[0].size(); int s1 = kk[1].size(); REP(i0,s0) REP(i1,s1){ int dif = abs(kk[0][i0].first - kk[1][i1].first) + abs(kk[0][i0].second - kk[1][i1].second); minv = min(minv, dif); } cout << minv-1 << endl; return 0; }