import itertools from collections import deque W, H = map(int, input().split()) C = [input() for _ in range(H)] table = [[None] * W for _ in range(H)] for h, w in itertools.product(range(1, H - 1), range(1, W - 1)): if C[h][w] == '.': h0, w0 = h, w break # bfs(連結部分) D0 = deque([(h0, w0)]) D = deque() table[h0][w0] = 0 while D0: h, w = D0.popleft() for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)): if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None: if C[h + dh][w + dw] == '.': D0.append((h + dh, w + dw)) table[h + dh][w + dw] = 0 else: D.append((h + dh, w + dw)) table[h + dh][w + dw] = 1 # bfs(壁) while D: h, w = D.popleft() for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)): if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None: D.append((h + dh, w + dw)) if C[h + dh][w + dw] == '.': print(table[h][w]) exit() else: table[h + dh][w + dw] = table[h][w] + 1