#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(I,A,B) for(int I = (A); I < (B); ++I) typedef long long ll; vector C; int W,H; bool visited[20][20]; int gr[20][20]; int g; void dfs(int y, int x) { if(y < 0 || y >= H || x < 0 || x >= W) return; if(visited[y][x]) return; visited[y][x] = true; if(C[y][x] == '#') return; gr[y][x] = g; dfs(y+1, x); dfs(y-1, x); dfs(y, x+1); dfs(y, x-1); return; } int main() { cin >> W >> H; FOR(y,0,H) { FOR(x,0,W) { visited[y][x] = false; gr[y][x] = -1; } } FOR(i,0,H) { string s; cin >> s; C.push_back(s); } g = 0; FOR(y,0,H) { FOR(x,0,W) { if(!visited[y][x]) { g++; dfs(y,x); } } } int ans = 1000; FOR(y1,0,H) { FOR(x1,0,W) { FOR(y2,0,H) { FOR(x2,0,W) { if(y1 == y2 && x1 == x2) continue; if(C[y1][x1] == '#' || C[y2][x2] == '#') continue; if(gr[y1][x1] != gr[y2][x2]) { ans = min(ans, abs(y1-y2) + abs(x1-x2)); } } } } } cout << ans - 1 << endl; return 0; }