#include int W, H; std::string str[32]; void dfs(int x, int y) { int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; str[y][x] = '1'; for(int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if( 0 <= nx and nx < W and 0 <= ny and ny < H ) { if( str[ny][nx] == '.' ) { str[ny][nx] = '1'; dfs(nx, ny); } } } return; } int main() { std::cin >> W >> H; for(int i = 0; i < H; ++i) { std::cin >> str[i]; } for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { if( str[i][j] == '.' ) { dfs(j, i); goto label_1; } } } label_1:; int ans = 100000; for(int x1 = 0; x1 < W; ++x1) { for(int y1 = 0; y1 < H; ++y1) { for(int x2 = 0; x2 < W; ++x2) { for(int y2 = 0; y2 < H; ++y2) { if( str[y1][x1] == '.' and str[y2][x2] == '1' ) { ans = std::min(ans, abs(x2 - x1) + abs(y2 - y1) - 1); } } } } } std::cout << ans << std::endl; return 0; }