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)
INF = 10 ** 10
dist = [INF] * (H * W)
for s in range(H * W):
    if len(G[s]) and dist[s] == INF:
        Q = collections.deque([s])
        se = set()
        while True:
            if not Q:
                break
            i = Q.popleft()
            for j in G[i]:
                if j in se:
                    continue
                se.add(j)
                k = j
                Q += [j]
        dist[k] = 0
        se = set()
        Q = collections.deque([k])
        while True:
            if not Q:
                break
            i = Q.popleft()
            if i in se:
                break
            se.add(i)
            d = dist[i]
            for j in G[i]:
                if dist[j] != INF:
                    continue
                k = j
                dist[j] = d + 1
                Q += [j]
        print(dist[k])
        exit()
