import numpy as np
H,W = map(int, input().split())
S = []
for i in range(H):
  tmp = input()
  S.append(tmp)

S_label = np.zeros([H,W], dtype=int)
label = 1
for h in range(H):
  for w in range(W):
    S_label[h][w] = label
    label+=1
root = []
for h in range(H):
  for w in range(W):
    if S[h][w] == '.':
      tmp_h, tmp_w = h, w+1	# 右
      #print(tmp_h, tmp_w)
      if tmp_w < W and S[tmp_h][tmp_w] == '.':
        root.append([S_label[h][w],S_label[tmp_h][tmp_w]])
      tmp_h, tmp_w = h+1, w	# 下
      if tmp_h < H and S[tmp_h][tmp_w] == '.':
        root.append([S_label[h][w],S_label[tmp_h][tmp_w]])
#print(root)

Q = [1]
visited = np.zeros(H*W, dtype=int)
visited[0] = 1
depth = 2
while len(Q) != 0:
  q = Q.pop(0)
  for i in range(len(root)):
    if q == root[i][0] and visited[root[i][1]-1] == 0:
      depth = visited[root[i][0]-1]+1
      visited[root[i][1]-1] = depth
      Q.append(root[i][1])
    elif q == root[i][1] and visited[root[i][0]-1] == 0:
      depth = visited[root[i][1]-1]+1
      visited[root[i][0]-1] = depth
      Q.append(root[i][0])
  depth += 1
print(max(visited)-1)