H,W=map(int,input().split())
maze=[]
maze.append(['#']*(W+2))
for i in range(H):
    tmp=['#']+list(input())+['#']
    if '.' in tmp:
        a,b=i,tmp.index('.')
    maze.append(tmp)
maze.append(['#']*(W+2))
infty=10**5

def far(a,b):
    cost[a][b]=0
    point[0].append((a,b))
    stocks=1
    while stocks>0:
        stocks-=1
        for i in range(infty):
            if point[i]==[]:
                continue
            else:
                a,b=point[i].pop(0)
                break
        if maze[a][b+1]=='.' and cost[a][b+1]==infty:
            cost[a][b+1]=i+1
            point[i+1].append((a,b+1))
            stocks+=1
        if maze[a][b-1]=='.' and cost[a][b-1]==infty:
            cost[a][b-1]=i+1
            point[i+1].append((a,b-1))
            stocks+=1
        if maze[a-1][b]=='.' and cost[a-1][b]==infty:
            cost[a-1][b]=i+1
            point[i+1].append((a-1,b))
            stocks+=1
        if maze[a+1][b]=='.' and cost[a+1][b]==infty:
            cost[a+1][b]=i+1
            point[i+1].append((a+1,b))
            stocks+=1
    return (a,b),i


cost=[[infty]*(W+2) for i in range(H+2)]
point=[[] for i in range(infty)]

a,b=far(a,b)[0]

cost=[[infty]*(W+2) for i in range(H+2)]
point=[[] for i in range(infty)]

print(far(a,b)[1])