# BFS queue
import queue
H, W = map(int,input().split())
b = [list(input()) for _ in range(H)]
dist = [[10**9 for _ in range(W)] for _ in range(H)]
start = (-1,-1)
end = (0,0)
# start把握
for i in range(H):
    for j in range(W):
        if b[i][j] == '.':
            start = (i,j)
            dist[i][j]=0
            break
    if start != (-1,-1):
        break
far = 0
move = [(-1,0), (0,-1), (1,0), (0,1)]
q = queue.Queue()
q.put(start)
# BFS
while 1:
    now = q.get()
    x = now[0]
    y = now[1]
    for elem in move:
        dx = elem[0]
        dy = elem[1]
        if x+dx>=0 and x+dx<= H-1 and y+dy>=0 and y+dy<=W-1 and abs(dx-dy)==1:
            if b[x+dx][y+dy] != '#':
                if dist[x+dx][y+dy] > dist[x][y]:
                    dist[x+dx][y+dy] = dist[x][y] + 1
                    if dist[x+dx][y+dy] > far:
                        far = dist[x+dx][y+dy]
                        far2 = (x+dx,y+dy)
                    q.put((x+dx,y+dy))
    if q.empty():
        break
dist = [[10**9 for _ in range(W)] for _ in range(H)]
q = queue.Queue()
q.put(far2)
dist[far2[0]][far2[1]]=0
far=0
# BFS
while 1:
    now = q.get()
    x = now[0]
    y = now[1]
    for elem in move:
        dx = elem[0]
        dy = elem[1]
        if x+dx>=0 and x+dx<= H-1 and y+dy>=0 and y+dy<=W-1 and abs(dx-dy)==1:
            if b[x+dx][y+dy] != '#':
                if dist[x+dx][y+dy] > dist[x][y]:
                    dist[x+dx][y+dy] = dist[x][y] + 1
                    if dist[x+dx][y+dy] > far:
                        far = dist[x+dx][y+dy]
                        far2 = (i,j)
                    q.put((x+dx,y+dy))
    if q.empty():
        break
print(far)
