import sys

input = sys.stdin.readline

 

def warshal_floyd(d, n):

  for k in range(n):

    for i in range(n):

      for j in range(n):

        d[i][j] = min(d[i][j], d[i][k]+d[k][j])

        

  return d



h, w = list(map(int, input().split()))

m = [str(eval(input())) for _ in range(h)]

d = [[float('inf') for i in range(h*w)] for j in range(h*w)]

for i in range(h*w):

  d[i][i] = 0

  

for i in range(h*w):

  for j in range(h*w):

    py, px = i//w, i%w

    qy, qx = j//w, j%w

    if (px == qx and abs(py-qy) == 1) or (py == qy and abs(px-qx) == 1):

      pm = m[py][px]

      qm = m[qy][qx]

      if qm in ['.'] and pm in ['.']:

        d[i][j] = 1

        

#print(d)

d = warshal_floyd(d, h*w)

#print(d)



ans = 0

for i in range(h*w):

  for j in range(h*w):

    if d[i][j] != float('inf'):

      ans = max(ans, d[i][j])

print(ans)
h, w = list(map(int, input().split()))



M = [str(eval(input())) for _ in range(h)]

M = ['#'*w] + M + ['#'*w]

M = ['#'+c+'#' for c in M]



from collections import deque

ans = 0

for sy in range(1, h+1):

    for sx in range(1, w+1):

        if M[sy][sx] == '#':

            continue

        q = deque()

        q.append((sy, sx))

        visit = [[-1]*(w+2) for _ in range(h+2)]

        visit[sy][sx] = 0

        while q:

            y, x = q.popleft()

            for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):

                ny, nx = y+dy, x+dx

                if visit[ny][nx] == -1 and M[ny][nx] != '#':

                    visit[ny][nx] = visit[y][x]+1

                    q.append((ny, nx))

        ans = max(ans, max(max(row) for row in visit))

print(ans)
import sys

input = sys.stdin.readline


def warshal_floyd(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


h, w = list(map(int, input().split()))
m = [str(eval(input())) for _ in range(h)]
d = [[float("inf") for i in range(h * w)] for j in range(h * w)]
for i in range(h * w):
    d[i][i] = 0
for i in range(h * w):
    for j in range(h * w):
        py, px = i // w, i % w
        qy, qx = j // w, j % w
        if (px == qx and abs(py - qy) == 1) or (py == qy and abs(px - qx) == 1):
            pm = m[py][px]
            qm = m[qy][qx]
            if qm in ["."] and pm in ["."]:
                d[i][j] = 1
# print(d)
d = warshal_floyd(d, h * w)
# print(d)
ans = 0
for i in range(h * w):
    for j in range(h * w):
        if d[i][j] != float("inf"):
            ans = max(ans, d[i][j])
print(ans)

h, w = list(map(int, input().split()))
M = [str(eval(input())) for _ in range(h)]
M = ["#" * w] + M + ["#" * w]
M = ["#" + c + "#" for c in M]
from collections import deque

ans = 0
for sy in range(1, h + 1):
    for sx in range(1, w + 1):
        if M[sy][sx] == "#":
            continue
        q = deque()
        q.append((sy, sx))
        visit = [[-1] * (w + 2) for _ in range(h + 2)]
        visit[sy][sx] = 0
        while q:
            y, x = q.popleft()
            for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
                ny, nx = y + dy, x + dx
                if visit[ny][nx] == -1 and M[ny][nx] != "#":
                    visit[ny][nx] = visit[y][x] + 1
                    q.append((ny, nx))
        ans = max(ans, max(max(row) for row in visit))
print(ans)

-import sys +h, w = list(map(int, input().split())) +M = [str(eval(input())) for _ in range(h)] +M = ["#" * w] + M + ["#" * w] +M = ["#" + c + "#" for c in M] +from collections import deque -input = sys.stdin.readline - - -def warshal_floyd(d, n): -    for k in range(n): -        for i in range(n): -            for j in range(n): -                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) -    return d - - -h, w = list(map(int, input().split())) -m = [str(eval(input())) for _ in range(h)] -d = [[float("inf") for i in range(h * w)] for j in range(h * w)] -for i in range(h * w): -    d[i][i] = 0 -for i in range(h * w): -    for j in range(h * w): -        py, px = i // w, i % w -        qy, qx = j // w, j % w -        if (px == qx and abs(py - qy) == 1) or (py == qy and abs(px - qx) == 1): -            pm = m[py][px] -            qm = m[qy][qx] -            if qm in ["."] and pm in ["."]: -                d[i][j] = 1 -# print(d) -d = warshal_floyd(d, h * w) -# print(d) -for i in range(h * w): -    for j in range(h * w): -        if d[i][j] != float("inf"): -            ans = max(ans, d[i][j]) +for sy in range(1, h + 1): +    for sx in range(1, w + 1): +        if M[sy][sx] == "#": +            continue +        q = deque() +        q.append((sy, sx)) +        visit = [[-1] * (w + 2) for _ in range(h + 2)] +        visit[sy][sx] = 0 +        while q: +            y, x = q.popleft() +            for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1): +                ny, nx = y + dy, x + dx +                if visit[ny][nx] == -1 and M[ny][nx] != "#": +                    visit[ny][nx] = visit[y][x] + 1 +                    q.append((ny, nx)) +        ans = max(ans, max(max(row) for row in visit))