import java.util.ArrayDeque; import java.util.Queue; import java.util.Scanner; public class Main_yukicoder157 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] dx = {1, -1, 0, 0}; int[] dy = {0, 0, 1, -1}; int w = sc.nextInt(); int h = sc.nextInt(); char[][] maze = new char[h][]; for (int i = 0; i < h; i++) { maze[i] = sc.next().toCharArray(); } int[][] cnt = new int[h][w]; int x = 0, y = 0; out: for (x = 0; x < w; x++) { for (y = 0; y < h; y++) { if (maze[y][x] == '.') { break out; } } } Queue qx = new ArrayDeque(); Queue qy = new ArrayDeque(); qx.add(x); qy.add(y); cnt[y][x] = 1; while (!qx.isEmpty()) { x = qx.remove(); y = qy.remove(); for (int i = 0; i < dx.length; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) { continue; } if (maze[ny][nx] == '#' || cnt[ny][nx] != 0) { continue; } cnt[ny][nx] = 1; qx.add(nx); qy.add(ny); } } int min = Integer.MAX_VALUE; for (int ix = 0; ix < w; ix++) { for (int iy = 0; iy < h; iy++) { if (maze[iy][ix] == '#' || cnt[iy][ix] != 1) { continue; } for (int jx = 0; jx < w; jx++) { for (int jy = 0; jy < h; jy++) { if (maze[jy][jx] == '#' || cnt[jy][jx] != 0) { continue; } min = Math.min(min, Math.abs(ix - jx) + Math.abs(iy - jy)); } } } } System.out.println(min - 1); sc.close(); } }