import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Scanner; import java.util.TreeSet; public class Main { public static final int INF = Integer.MAX_VALUE / 2 - 1; public static final int[] vs = {1, 0, -1, 0}; public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int W = sc.nextInt(); final int H = sc.nextInt(); boolean[][] is_wall = new boolean[H][W]; for(int i = 0; i < H; i++){ char[] inputs = sc.next().toCharArray(); for(int j = 0; j < W; j++){ is_wall[i][j] = inputs[j] == '#'; } } int sx = -1, sy = -1; LOOP: for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(!is_wall[i][j]){ sx = j; sy = i; break LOOP; } } } int[][] dists = new int[H][W]; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ dists[i][j] = INF; } } dists[sy][sx] = 0; LinkedList y_queue = new LinkedList(); LinkedList x_queue = new LinkedList(); y_queue.add(sy); x_queue.add(sx); while(!y_queue.isEmpty()){ final int y = y_queue.poll(); final int x = x_queue.poll(); if(dists[y][x] != 0 && !is_wall[y][x]){ System.out.println(dists[y][x]); break; } for(int v = 0; v < vs.length; v++){ final int nx = x + vs[v]; final int ny = y + vs[(v + 1) % vs.length]; if(nx < 0 || nx >= W || ny < 0 || ny >= H){ continue; } if(is_wall[ny][nx] && dists[ny][nx] > dists[y][x] + 1){ dists[ny][nx] = Math.min(dists[ny][nx], dists[y][x] + 1); y_queue.addLast(ny); x_queue.addLast(nx); } if(!is_wall[ny][nx] && dists[ny][nx] > dists[y][x]){ dists[ny][nx] = Math.min(dists[ny][nx], dists[y][x]); y_queue.addFirst(ny); x_queue.addFirst(nx); } } } } }