import java.util.*; import java.awt.geom.*; import java.io.*; public class Main { static int[] vx = {1,0,-1,0}; static int[] vy = {0,1,0,-1}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); char[][] map = new char[h][]; for(int i = 0; i < h; i++) { map[i] = sc.next().toCharArray(); } int[][] sMap = new int[h][w]; int count = 0; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(sMap[i][j] == 0 && map[i][j] == '.') dfsA(i,j,++count,sMap,map); } } int MIN = 2 << 27; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(map[i][j] == '#' || sMap[i][j] != 1) continue; MIN = Math.min(MIN, bfs(i,j,1,sMap,map)); } } System.out.println(MIN); } static int bfs(int y, int x, int f, int[][] sMap, char[][] map) { ArrayDeque queue = new ArrayDeque(); int[][] cpM = new int[sMap.length][]; for(int i = 0; i < cpM.length; i++) { cpM[i] = Arrays.copyOf(sMap[i],sMap[i].length); } queue.add(new Data(y,x,0,cpM)); cpM[y][x] = 1; while(!queue.isEmpty()) { Data tmp = queue.pollFirst(); for(int i = 0; i < 4; i++) { int tx = vx[i] + tmp.x; int ty = vy[i] + tmp.y; if(tx < 0 || ty < 0 || ty >= map.length || tx >= map[ty].length) continue; if(tmp.sMap[ty][tx] == 2) return tmp.count; if(tmp.sMap[ty][tx] == 1) continue; int[][] cpMx = new int[sMap.length][]; for(int j = 0; j < cpM.length; j++) { cpMx[j] = Arrays.copyOf(tmp.sMap[j],tmp.sMap[j].length); } cpMx[ty][tx] = 1; queue.add(new Data(ty,tx,tmp.count+1,cpMx)); } } return 2 << 27; } static class Data { int y; int x; int count; int[][] sMap; Data(int a, int b,int c, int[][] d) { y = a; x = b; count = c; sMap = d; } } static void dfsA(int y,int x, int f, int[][] sMap, char[][] map) { sMap[y][x] = f; for(int i = 0; i < 4; i++) { int tx = vx[i] + x; int ty = vy[i] + y; if(tx < 0 || ty < 0 || ty >= map.length || tx >= map[ty].length || map[ty][tx] == '#' || sMap[ty][tx] != 0) continue; dfsA(ty,tx,f,sMap,map); } } }