H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
#print(S)
idx = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      S[i][j] = idx
      idx += 1
#print(S)
D = [[float('inf') for _ in range(idx)] for _ in range(idx)]
for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      continue
    for dy,dx in [(-1,0),(1,0),(0,-1),(0,1)]:
      if 0 <= i+dy < H and 0 <= j+dx < W and S[i+dy][j+dx] != '#':
        D[S[i][j]][S[i+dy][j+dx]] = 1
        D[S[i+dy][j+dx]][S[i][j]] = 1
for i in range(idx):
  D[i][i] = 0

for k in range(idx):
  for i in range(idx):
    for j in range(idx):
      D[i][j] = min(D[i][k]+D[k][j],D[i][j])
#print(D)
ans = 0
for d in D:
  ans = max(max(d),ans)
print(ans)