from collections import deque w, h = map(int, input().split()) S = [list(input()) for _ in range(h)] ci, cj = None, None for i in range(h): for j in range(w): if S[i][j] == '.': ci, cj = i, j break if ci is not None: break Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] Que = deque([(ci, cj)]) start_set = set() start_set.add((ci, cj)) while Que: ci, cj = Que.popleft() for di, dj in Directions: ni, nj = ci + di, cj + dj if 0 <= ni < h and 0 <= nj < w: if S[ni][nj] == '.' and (ni, nj) not in start_set: start_set.add((ni, nj)) Que.append((ni, nj)) Que = deque() for ci, cj in start_set: Que.append((ci, cj, 0)) seen = set() while Que: ci, cj, cnt = Que.popleft() if S[ci][cj] == '.' and (ci, cj) not in start_set: print(cnt - 1) break for di, dj in Directions: ni, nj = ci + di, cj + dj if 0 <= ni < h and 0 <= nj < w: if (ni, nj) not in start_set and (ni, nj) not in seen: seen.add((ni, nj)) Que.append((ni, nj, cnt + 1))