import itertools
import sys
sys.setrecursionlimit(10**5)

H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

# H 3 W 5

step = 0
pattern = [[-1, 0], [1, 0], [0, 1], [0, -1]]
result = []
current_positions = [[0, 0]]
while current_positions:
    next_position = []
    step += 1
    for c_p in current_positions:
        ifStuck = True
        for pat in pattern:
            n_p = [c_p[0] + pat[0], c_p[1] + pat[1]]
            if n_p[0] >= 0 and n_p[0] < H and n_p[1] >= 0 and n_p[1] < W:
                if field[n_p[0]][n_p[1]] == '.':
                    field[n_p[0]][n_p[1]] = '#'
                    next_position.append(n_p)
                    ifStuck = False
        if ifStuck:
            result.append([step-1, c_p])
    # think when its unreachable
    current_positions = next_position
result.sort()
result.reverse()
print(result[0][0] + result[1][0])


# 計算量も考えたつもりだけど盛大にTLE。どのようなケースのことを考えられていなかったかを明確にして次に進むこと。
# 別の回答案はipadに記述済み
