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 class UnionFind: def __init__(self, n): self._tree = [-1]*n # number of connected component self.cnt = n def root(self, u): stack = [] while self._tree[u] >= 0: stack.append(u) u = self._tree[u] for v in stack: self._tree[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u, v = self.root(u), self.root(v) if u == v: return False if self._tree[u] > self._tree[v]: u, v = v, u self._tree[u] += self._tree[v] self._tree[v] = u self.cnt -= 1 return True # size of connected component def size(self, u): return -self._tree[self.root(u)] 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) s = h*w t = s+1 uf = UnionFind(h*w+2) i = 0 for j in range(w): if ss[i][j]: uf.merge(s, i*w+j) j = w-1 for i in range(h): if ss[i][j]: uf.merge(s, i*w+j) i = h-1 for j in range(w): if ss[i][j]: uf.merge(t, i*w+j) j = 0 for i in range(h): if ss[i][j]: uf.merge(t, i*w+j) for i in range(h): for j in range(w): if i+1 < h and ss[i][j] and ss[i+1][j]: uf.merge(i*w+j, (i+1)*w+j) if j+1 < w and ss[i][j] and ss[i][j+1]: uf.merge(i*w+j, i*w+j+1) if uf.same(s, t): print(0) exit() def ok(i, j): res = 0 if i<2 or j>=w-2:res|=1 if i>=h-2 or j<2:res|=2 for di in range(-2, 3): for dj in range(-2, 3): if (di,dj)==(-2,-2) or (di,dj)==(2,-2) or (di,dj)==(-2,2) or (di,dj)==(2,2):continue ni, nj = i+di, j+dj if ni < 0 or nj < 0 or ni >= h or nj >= w: continue if uf.same(ni*w+nj, s): res |= 1 if uf.same(ni*w+nj, t): res |= 2 if res == 3: return True return False for i in range(1, h-1): for j in range(1, w-1): if i < 3 and j < 3: continue if i >= h-3 and j >= w-3: continue if ok(i, j): print(1) exit() print(2)