import collections
H, W, *S = open(0).read().split()
H,W=[int(_) for _ in [H,W]]
G = [set() for _ in range(H * W)]
for i in range(H * W):
    x, y = divmod(i, W)
    if S[x][y] == '.':
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            nx, ny = x + dx, y + dy
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            if S[nx][ny] == '.':
                G[i].add(nx * W + ny)
for s in range(H * W):
    if len(G[s]):
        break
Q = collections.deque([s])
INF = 10 ** 10
d1 = [INF] * (H * W)
d1[s] = 0
while True:
    if not Q:
        break
    i = Q.popleft()
    d = d1[i]
    for j in G[i]:
        if d1[j] != INF:
            continue
        k = j
        d1[j] = d + 1
        Q.appendleft(j)
d1 = [INF] * (H * W)
d1[k] = 0
Q = collections.deque([k])
while True:
    if not Q:
        break
    i = Q.popleft()
    d = d1[i]
    for j in G[i]:
        if d1[j] != INF:
            continue
        d1[j] = d + 1
        Q.appendleft(j)
print(d1[k])
