#include #include using namespace std; using ll = long long; using P = pair; #define mkp make_pair P ar4[4] = {mkp(0,1),mkp(0,-1),mkp(1,0),mkp(-1,0)}; vector> A(21,vector(21)); vector

X,Y; void dfs(int y,int x,int v){ A[y][x] = '#'; if(v==0)X.push_back({x,y}); else Y.push_back({x,y}); for(int i = 0; 4 > i; i++){ if(A[y+ar4[i].first][x+ar4[i].second] == '.'){ dfs(y+ar4[i].first,x+ar4[i].second,v); } } } int main(){ int n,m;cin>>n>>m; for(int i = 0; m > i; i++){ for(int j = 0; n > j; j++){ cin>>A[i][j]; } } for(int i = 0; m > i; i++){ for(int j = 0; n > j; j++){ if(A[i][j] == '.' && !X.size()){ dfs(i,j,0); }else if(A[i][j] == '.'){ dfs(i,j,1); } } } // cout << X.size() << " " << Y.size() << endl; // for(int i = 0; X.size() > i; i++){ // cout << X[i].first << " " << X[i].second << endl; // } // cout << endl; // for(int i = 0; Y.size() > i; i++){ // cout << Y[i].first << " " << Y[i].second << endl; // } ll mn = 200; for(int i = 0; X.size() > i; i++){ for(int j = 0; Y.size() > j; j++){ mn = min(mn,abs(X[i].first-Y[j].first)+abs(X[i].second-Y[j].second)); } } cout << mn-1 << endl; }