from collections import deque def canReach(H, W, Ss): grid = [[False for _ in range(W - 2)] for _ in range(H - 2)] for h in range(1, H - 1): for w in range(1, W - 1): grid[h - 1][w - 1] = True for dh in range(-1, 2): for dw in range(-1, 2): if Ss[h + dh][w + dw] == "#": grid[h - 1][w - 1] = False # 0,0からH-3,W-3までの経路があるか adj = [[[] for _ in range(W - 2)] for _ in range(H - 2)] for h in range(H - 2): for w in range(W - 2): if not grid[h][w]: continue for dh in range(-1, 2): for dw in range(-1, 2): if dh == 0 and dw == 0: continue if ( 0 <= h + dh < H - 2 and 0 <= w + dw < W - 2 and grid[h + dh][w + dw] ): adj[h][w].append((h + dh, w + dw)) q = deque([(0, 0)]) visited = [[False for _ in range(W - 2)] for _ in range(H - 2)] visited[0][0] = True while q: h, w = q.popleft() for nh, nw in adj[h][w]: if not visited[nh][nw]: visited[nh][nw] = True q.append((nh, nw)) return visited[H - 3][W - 3] def main(): H, W = map(int, input().split()) assert 6 <= H <= 500 assert 6 <= W <= 500 Ss = [] for _ in range(H): S = input().strip() assert len(S) == W for c in S: assert c == "." or c == "#" Ss.append(S) for i in range(3): for j in range(3): assert Ss[i][j] == "." for i in range(H - 3, H): for j in range(W - 3, W): assert Ss[i][j] == "." ans = 2 if not canReach(H, W, Ss): ans = 0 else: for h in range(H): for w in range(W): if h < 3 and w < 3: continue if h >= H - 3 and w >= W - 3: continue if Ss[h][w] == "#": continue Ss[h] = Ss[h][:w] + "#" + Ss[h][w + 1 :] if not canReach(H, W, Ss): ans = 1 Ss[h] = Ss[h][:w] + "." + Ss[h][w + 1 :] print(ans) if __name__ == "__main__": main()