import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(1000005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 from collections import deque h, w = LI() ss = [[(c == "#")*1 for c in SI()] for _ in range(h)] for i in range(h): for j in range(w): if ss[i][j] == 1: for di, dj in dij: ni, nj = i+di, j+dj if ni < 0 or nj < 0 or ni >= h or nj >= w: continue if ss[ni][nj] == 0: ss[ni][nj] = 2 # p2D(ss) q = deque() dist = [[inf]*w for _ in range(h)] for j in range(w): if ss[0][j]: q.append((0, j)) dist[0][j] = 0 for i in range(h): if ss[i][w-1]: q.append((i, w-1)) dist[i][w-1] = 0 while q: i, j = q.popleft() d = dist[i][j] for di, dj in dij: ni, nj = i+di, j+dj if ni < 0 or nj < 0 or ni >= h or nj >= w: continue if dist[ni][nj] == inf: if d == 0 and ss[ni][nj]: dist[ni][nj] = 0 q.appendleft((ni,nj)) else: dist[ni][nj] = d+1 q.append((ni, nj)) st = [] for i in range(h): if ss[i][0]: if dist[i][0] == 0: print(0) exit() if dist[i][0] < 5: print(1) exit() st.append((i, 0)) for j in range(w): if ss[h-1][j]: if dist[h-1][j] == 0: print(0) exit() if dist[h-1][j] < 5: print(1) exit() st.append((h-1, j)) while st: i, j = st.pop() for di, dj in dij: ni, nj = i+di, j+dj if ni < 0 or nj < 0 or ni >= h or nj >= w: continue if ss[ni][nj] and dist[ni][nj] != inf: if dist[ni][nj] < 5: print(1) exit() dist[ni][nj] = inf st.append((ni, nj)) print(2)