import numpy as np
from scipy.sparse import csgraph
H, W = map(int, input().split())
maze = np.array([c == "." for c in "".join(input() for _ in range(H))], dtype=np.bool)
array = np.zeros((H * W, H * W), dtype=np.bool)
N = np.arange(H * W)
R, C = np.divmod(N, W)
c_i = N[maze[N] & (C > 0) & maze[N - 1]]
r_i = N[maze[N] & (R > 0) & maze[N - W]]
array[c_i, c_i - 1] = 1
array[c_i - 1, c_i] = 1
array[r_i, r_i - W] = 1
array[r_i - W, r_i] = 1
dist = csgraph.shortest_path(array)
print(int(dist[dist < 401].max()))
