from collections import defaultdict as dd, deque

h,w = map(int,input().split())
M = [input().strip()+'#' for _ in range(h)]
M.append(w*'#')

start = None
for i in range(h):
    for j in range(w):
        if M[i][j] == '.':
            start = (j,i)

Q = deque()
Q.append((0, *start))
seen = set()

while Q:
    d,x,y = Q.popleft()

    if (x,y) in seen: continue
    seen.add((x,y))

    for nx,ny in [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]:
        if M[ny][nx] == '.':
            Q.append((d+1, nx, ny))


Q = deque()
Q.append((0, x, y))
seen = set()

while Q:
    d,x,y = Q.popleft()

    if (x,y) in seen: continue
    seen.add((x,y))

    for nx,ny in [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]:
        if M[ny][nx] == '.':
            Q.append((d+1, nx, ny))

print(d)
