from copy import deepcopy
from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

M = []
vx = [0,1,0,-1]
vy = [1,0,-1,0]
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    for i in range(H):
        startX = M[i].index('.')
        if startX >= 0:
            startY = i
            break

    Mtemp = deepcopy(M)

    Mtemp[startY][startX] = '#'
    qx.append(startX)
    qy.append(startY)
    qc.append(0)
    cnt = 0
    x = 0 
    y = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[x-1][y] == '.':
                Mtemp[x-1][y] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[x+1][y] == '.':
                Mtemp[x+1][y] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[x][y-1] == '.':
                Mtemp[x][y-1] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[x][y+1] == '.':
                Mtemp[x][y+1] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    Mtemp = deepcopy(M)

    Mtemp[x][y] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[x-1][y] == '.':
                Mtemp[x-1][y] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[x+1][y] == '.':
                Mtemp[x+1][y] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[x][y-1] == '.':
                Mtemp[x][y-1] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[x][y+1] == '.':
                Mtemp[x][y+1] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)



    print(cnt)




if __name__ == '__main__':
    main()

