def dijkstra(g,st):
    from heapq import heappush, heappop, heapify
    inf=10**18
    dist=[inf for i in range(len(g))]
    visited=[False for i in range(len(g))]
    dist[st]=0
    que=[]
    heappush(que,(0,st))
    while que:
        c,v=heappop(que)
        if visited[v]:
            continue
        visited[v]=True
        for x in g[v]:
            tmp=x[0]+c
            if tmp<dist[x[1]] and not visited[x[1]]:
                dist[x[1]]=tmp
                heappush(que,(tmp,x[1]))
    return dist

h,w=map(int,input().split())
field=[[]for i in range(h)]
num=0
st=-1
for i in range(h):
    s=input()
    for j in range(w):
        field[i].append((s[j],num))
        if s[j]=='.' and st==-1:
            st=num
        num+=1
dx=[1,0,-1,0]
dy=[0,1,0,-1]

g=[[]for i in range(h*w)]
for i in range(h):
    for j in range(w):
        if field[i][j][0]!='.':
            continue
        for x,y in zip(dx,dy):
            if 0<=i+x<h and 0<=j+y<w and field[i+x][j+y][0]!='#':
                g[field[i][j][1]].append((1,field[i+x][j+y][1]))
mx=(-1,-1)
dd=dijkstra(g,st)
for i in range(len(dd)):
    if dd[i]!=10**18 and dd[i]>mx[0]:
        mx=(dd[i],i)
mxx=-1
for ans in dijkstra(g,mx[1]):
    if ans!=10**18:
        mxx=max(mxx,ans)
print(mxx)