from collections import deque

def a():
    H,W = map(int,input().split())
    S = ["" for _ in range(H)]
    for _ in range(H):
        S[_] = input()
    b = lambda n:S[n//W][n%W]
    u = (lambda n:(n%W,n//W))(min(range(H*W),key=b))

    def bfs(root):
        depth = [[-1]*W for _ in range(H)]
        q = deque()
        q.append((root,0))
        while q:
            r,d = q.popleft()
            x,y = r
            depth[y][x] = d
            for dx,dy in ((1,0),(-1,0),(0,1),(0,-1)):
                dx += x
                dy += y
                if 0 <= dx < W and 0 <= dy < H and depth[dy][dx] == -1 and S[dy][dx] == ".":
                    q.append(((dx,dy),d+1))
        return depth

    c = bfs(u)
    d = lambda n:c[n//W][n%W]
    v = (lambda n:(n%W,n//W))(max(range(H*W),key=d))

    e = bfs(v)
    f = lambda n:e[n//W][n%W]
    print(f(max(range(H,W),key=f)))



a()



