## https://yukicoder.me/problems/no/2412 from collections import deque def bfs(H, W, cum_s_array): passed = [[False] * (W - 2) for _ in range(H - 2)] passed[0][0] = True queue = deque() queue.append((0, 0)) while len(queue) > 0: h, w = queue.popleft() for dh in range(-1, 2): for dw in range(-1, 2): if dh == 0 and dw == 0: continue new_h = dh + h new_w = dw + w if 0 <= new_h < H - 2 and 0 <= new_w < W - 2: if cum_s_array[new_h + 3][new_w + 3] - cum_s_array[new_h][new_w + 3] - cum_s_array[new_h + 3][new_w] + cum_s_array[new_h][new_w] == 0: if not passed[new_h][new_w]: passed[new_h][new_w] = True queue.append((new_h, new_w)) return passed[-1][-1] def reachable(H, W, s_array): # 累積和計算 cum_s_array = [[0] * (W + 1) for _ in range(H + 1)] for h in range(H): cum_row = 0 for w in range(W): cum_row += s_array[h][w] cum_s_array[h + 1][w + 1] = cum_row + cum_s_array[h][w + 1] # BFSによる探索 return bfs(H, W, cum_s_array) def main(): H, W = map(int, input().split()) S = [] for _ in range(H): S.append(input()) # 迷路のベースを作成 s_array = [] for s in S: row = [] for i in range(len(s)): if s[i] == "#": row.append(1) else: row.append(0) s_array.append(row) if not reachable(H, W, s_array): print(0) return def forbidden(h, w): if 0 <= h < 3 and 0 <= w < 3: return True if H - 3 <= h < H and W - 3 <= w < W: return True return False for h in range(H): for w in range(W): if S[h][w] == ".": if not forbidden(h, w): s_array[h][w] = 1 if not reachable(H, W, s_array): print(1) return s_array[h][w] = 0 print(2) if __name__ == '__main__': main()