import sys input = sys.stdin.readline H, W = map(int, input().split()) S = [input().rstrip() for _ in range(H)] reachable1 = [[0] * W for _ in range(W)] st = [] for i in range(H): for j in range(W): if S[i][j] == '#' and (i >= H-3 or j < 3): reachable1[i][j] = 1 st.append((i, j)) while st: i, j = st.pop() for ni in range(i-3, i+4): for nj in range(j-3, j+4): if (ni, nj) != (i, j) and 0 <= ni < H and 0 <= nj < W and S[ni][nj] == '#' and not reachable1[ni][nj]: reachable1[ni][nj] = 1 st.append((ni, nj)) reachable2 = [[0] * W for _ in range(W)] st = [] for i in range(H): for j in range(W): if S[i][j] == '#' and (i < 3 or j >= W-3): reachable2[i][j] = 1 st.append((i, j)) while st: i, j = st.pop() for ni in range(i-3, i+4): for nj in range(j-3, j+4): if (ni, nj) != (i, j) and 0 <= ni < H and 0 <= nj < W and S[ni][nj] == '#' and not reachable2[ni][nj]: reachable2[ni][nj] = 1 st.append((ni, nj)) for i in range(H): for j in range(W): if reachable1[i][j] and reachable2[i][j]: print(0) exit() for i in range(H): for j in range(W): r1 = r2 = 0 for ni in range(i-3, i+4): for nj in range(j-3, j+4): if (ni, nj) != (i, j) and 0 <= ni < H and 0 <= nj < W: r1 |= reachable1[ni][nj] r2 |= reachable2[ni][nj] if r1 and r2: print(1) exit() print(2)