import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); char[][] field = new char[h][]; for (int i = 0; i < h; i++) { field[i] = sc.next().toCharArray(); } ArrayDeque deq = new ArrayDeque<>(); boolean[][] visited = new boolean[h][w]; deq.add(w + 1); while (deq.size() > 0) { int x = deq.poll(); int r = x / w; int c = x % w; if (visited[r][c]) { continue; } visited[r][c] = true; boolean hasDef = false; for (int i = -1; i <= 1 && !hasDef; i++) { for (int j = -1; j <= 1 && !hasDef; j++) { hasDef = (field[r + i][c + j] == '#'); } } if (hasDef) { continue; } if (r > 1) { deq.add(x - w); } if (r < h - 2) { deq.add(x + w); } if (c > 1) { deq.add(x - 1); } if (c < w - 2) { deq.add(x + 1); } } if (!visited[h - 2][w - 2]) { System.out.println(0); return; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i <= 2 && j <= 2) { continue; } if (i >= h - 3 && j >= w - 3) { continue; } if (field[i][j] == '#') { continue; } field[i][j] = '#'; deq.add(w + 1); for (boolean[] arr : visited) { Arrays.fill(arr, false); } while (deq.size() > 0) { int x = deq.poll(); int r = x / w; int c = x % w; if (visited[r][c]) { continue; } visited[r][c] = true; boolean hasDef = false; for (int a = -1; a <= 1 && !hasDef; a++) { for (int b = -1; b <= 1 && !hasDef; b++) { hasDef = (field[r + a][c + b] == '#'); } } if (hasDef) { continue; } if (r > 1) { deq.add(x - w); } if (r < h - 2) { deq.add(x + w); } if (c > 1) { deq.add(x - 1); } if (c < w - 2) { deq.add(x + 1); } } if (!visited[h - 2][w - 2]) { System.out.println(1); return; } field[i][j] = '.'; } } System.out.println(2); } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }