package No100番台; import java.util.Scanner; public class Q157 { public static void main(String[] args) throws Exception { new Q157().solve(); } int[] dx = { 1, -1, 0, 0 }; int[] dy = { 0, 0, 1, -1 }; void dfs(char[][] map, int i, int j, char x) { map[i][j] = x; for (int k = 0; k < 4; k++) { if (map[i + dy[k]][j + dx[k]] == '.') dfs(map, i + dy[k], j + dx[k], x); } } void solve() { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); char[][] map = new char[h][w]; for (int i = 0; i < h; i++) { map[i] = sc.next().toCharArray(); } bi: for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i][j] == '.') { dfs(map, i, j, '1'); break bi; } } } bi: for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i][j] == '.') { dfs(map, i, j, '2'); break bi; } } } int ans = 999999999; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i][j] == '1') { for (int k = 0; k < h; k++) { for (int l = 0; l < w; l++) { if (map[k][l] == '2') ans = Math.min(ans, Math.abs(i - k) + Math.abs(j - l)); } } } } } System.out.println(ans - 1); } }