
import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))

def is_inner(x,H,W):
    if np.all(0<=x) and np.all(x<(H,W)):
        return True
    else:
        return False
    
directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])
directions = list(map(np.array, directions))

def searchfrom(x):
    global H,W
    dp=[[9999 for j in range(W)]for i in range(H)]
    dp[x[0]][x[1]]=0
    queue=[x]
    while True:
        x=queue.pop()
        for d in directions:
            nx=x+d
            if is_inner(nx,H,W) and dp[nx[0]][nx[1]]>=dp[x[0]][x[1]]+1 and field[nx[0]][nx[1]]!="#":
                dp[nx[0]][nx[1]]=dp[x[0]][x[1]]+1
                queue.append(nx)
        
        if len(queue)==0:
            return dp

def solve_from(x):
    global field
    res = searchfrom(x)

    mv = -1
    for rr in res:
        rr=[r for r in rr if  r<=1000]
        tmp = max(rr)

        if tmp<1000:
            mv = max(mv, tmp)
    return mv






H,W=acinput()

field=[]
for i in range(H):
    field.append(list(input()))



print(max(solve_from([0,0]),solve_from([H-1,0]),solve_from([0,W-1]),solve_from([H-1,W-1])))

