# from collections import deque

H, W = list(map(int, input().split()))

# dp = [[1000 for _ in range(W)] for _ in range(H)]



S = [['#']*(W+2)] + [['#']+list(eval(input()))+['#'] for h in range(H)] + [['#']*(W+2)]

# S = (H+2, W+2)

# S =  [input().split() for h in range(H)]

max_n = 0



for h1 in range(1,H+1):

  for w1 in range(1,W+1):

    if S[h1][w1] == '#':

      continue

    distance = [[1000 for _ in range(W+2)] for _ in range(H+2)]

    done_que = [[0 for _ in range(W+2)] for _ in range(H+2)]

    distance[h1][w1] = 0

    que = []

    que.append([h1,w1])

    que_i = 0

    while que_i < len(que):

      h, w = que[que_i]

      if S[h-1][w] == '.' and done_que[h-1][w] == 0:

        done_que[h-1][w] = 1

        que.append([h-1,w])

        distance[h-1][w] = min(distance[h][w] + 1, distance[h-1][w])

      if S[h+1][w] == '.' and done_que[h+1][w] == 0:

        done_que[h+1][w] = 1

        que.append([h+1,w])

        distance[h+1][w] = min(distance[h][w] + 1, distance[h+1][w])

      if S[h][w-1] == '.' and done_que[h][w-1] == 0:

        done_que[h][w-1] = 1

        que.append([h,w-1])

        distance[h][w-1] = min(distance[h][w] + 1, distance[h][w-1])

      if S[h][w+1] == '.' and done_que[h][w+1] == 0:

        done_que[h][w+1] = 1

        que.append([h,w+1])

        distance[h][w+1] = min(distance[h][w] + 1, distance[h][w+1])

      que_i += 1

    hw_max_n = 0

    for h2 in range(1,H+1):

      for w2 in range(1,W+1):

        if distance[h2][w2] != 1000:

          hw_max_n = max(hw_max_n, distance[h2][w2])

    max_n = max(max_n, hw_max_n)

print(max_n)
from collections import deque

H, W = list(map(int, input().split()))



S = [['#']*(W+2)] + [['#']+list(eval(input()))+['#'] for h in range(H)] + [['#']*(W+2)]

max_n = 0



for h1 in range(1,H+1):

  for w1 in range(1,W+1):

    if S[h1][w1] == '#':

      continue

    distance = [[-1 for _ in range(W+2)] for _ in range(H+2)]

    distance[h1][w1] = 0

    que = deque([])

    que.append([h1,w1])

    while que:

      h, w = que.popleft()

      if S[h-1][w] == '.' and distance[h-1][w] == -1:

        que.append([h-1,w])

        distance[h-1][w] = distance[h][w] + 1

      if S[h+1][w] == '.' and distance[h+1][w] == -1:

        que.append([h+1,w])

        distance[h+1][w] = distance[h][w] + 1

      if S[h][w-1] == '.' and distance[h][w-1] == -1:

        que.append([h,w-1])

        distance[h][w-1] = distance[h][w] + 1

      if S[h][w+1] == '.' and distance[h][w+1] == -1:

        que.append([h,w+1])

        distance[h][w+1] = distance[h][w] + 1

    hw_max_n = max([max(dis) for dis in distance])

    max_n = max(max_n, hw_max_n)

print(max_n)
# from collections import deque
H, W = list(map(int, input().split()))
# dp = [[1000 for _ in range(W)] for _ in range(H)]
S = (
    [["#"] * (W + 2)]
    + [["#"] + list(eval(input())) + ["#"] for h in range(H)]
    + [["#"] * (W + 2)]
)
# S = (H+2, W+2)
# S =  [input().split() for h in range(H)]
max_n = 0
for h1 in range(1, H + 1):
    for w1 in range(1, W + 1):
        if S[h1][w1] == "#":
            continue
        distance = [[1000 for _ in range(W + 2)] for _ in range(H + 2)]
        done_que = [[0 for _ in range(W + 2)] for _ in range(H + 2)]
        distance[h1][w1] = 0
        que = []
        que.append([h1, w1])
        que_i = 0
        while que_i < len(que):
            h, w = que[que_i]
            if S[h - 1][w] == "." and done_que[h - 1][w] == 0:
                done_que[h - 1][w] = 1
                que.append([h - 1, w])
                distance[h - 1][w] = min(distance[h][w] + 1, distance[h - 1][w])
            if S[h + 1][w] == "." and done_que[h + 1][w] == 0:
                done_que[h + 1][w] = 1
                que.append([h + 1, w])
                distance[h + 1][w] = min(distance[h][w] + 1, distance[h + 1][w])
            if S[h][w - 1] == "." and done_que[h][w - 1] == 0:
                done_que[h][w - 1] = 1
                que.append([h, w - 1])
                distance[h][w - 1] = min(distance[h][w] + 1, distance[h][w - 1])
            if S[h][w + 1] == "." and done_que[h][w + 1] == 0:
                done_que[h][w + 1] = 1
                que.append([h, w + 1])
                distance[h][w + 1] = min(distance[h][w] + 1, distance[h][w + 1])
            que_i += 1
        hw_max_n = 0
        for h2 in range(1, H + 1):
            for w2 in range(1, W + 1):
                if distance[h2][w2] != 1000:
                    hw_max_n = max(hw_max_n, distance[h2][w2])
        max_n = max(max_n, hw_max_n)
print(max_n)

from collections import deque

H, W = list(map(int, input().split()))
S = (
    [["#"] * (W + 2)]
    + [["#"] + list(eval(input())) + ["#"] for h in range(H)]
    + [["#"] * (W + 2)]
)
max_n = 0
for h1 in range(1, H + 1):
    for w1 in range(1, W + 1):
        if S[h1][w1] == "#":
            continue
        distance = [[-1 for _ in range(W + 2)] for _ in range(H + 2)]
        distance[h1][w1] = 0
        que = deque([])
        que.append([h1, w1])
        while que:
            h, w = que.popleft()
            if S[h - 1][w] == "." and distance[h - 1][w] == -1:
                que.append([h - 1, w])
                distance[h - 1][w] = distance[h][w] + 1
            if S[h + 1][w] == "." and distance[h + 1][w] == -1:
                que.append([h + 1, w])
                distance[h + 1][w] = distance[h][w] + 1
            if S[h][w - 1] == "." and distance[h][w - 1] == -1:
                que.append([h, w - 1])
                distance[h][w - 1] = distance[h][w] + 1
            if S[h][w + 1] == "." and distance[h][w + 1] == -1:
                que.append([h, w + 1])
                distance[h][w + 1] = distance[h][w] + 1
        hw_max_n = max([max(dis) for dis in distance])
        max_n = max(max_n, hw_max_n)
print(max_n)

-# from collections import deque +from collections import deque + -# dp = [[1000 for _ in range(W)] for _ in range(H)] -# S = (H+2, W+2) -# S =  [input().split() for h in range(H)] -        distance = [[1000 for _ in range(W + 2)] for _ in range(H + 2)] -        done_que = [[0 for _ in range(W + 2)] for _ in range(H + 2)] +        distance = [[-1 for _ in range(W + 2)] for _ in range(H + 2)] -        que = [] +        que = deque([]) -        que_i = 0 -        while que_i < len(que): -            h, w = que[que_i] -            if S[h - 1][w] == "." and done_que[h - 1][w] == 0: -                done_que[h - 1][w] = 1 +        while que: +            h, w = que.popleft() +            if S[h - 1][w] == "." and distance[h - 1][w] == -1: -                distance[h - 1][w] = min(distance[h][w] + 1, distance[h - 1][w]) -            if S[h + 1][w] == "." and done_que[h + 1][w] == 0: -                done_que[h + 1][w] = 1 +                distance[h - 1][w] = distance[h][w] + 1 +            if S[h + 1][w] == "." and distance[h + 1][w] == -1: -                distance[h + 1][w] = min(distance[h][w] + 1, distance[h + 1][w]) -            if S[h][w - 1] == "." and done_que[h][w - 1] == 0: -                done_que[h][w - 1] = 1 +                distance[h + 1][w] = distance[h][w] + 1 +            if S[h][w - 1] == "." and distance[h][w - 1] == -1: -                distance[h][w - 1] = min(distance[h][w] + 1, distance[h][w - 1]) -            if S[h][w + 1] == "." and done_que[h][w + 1] == 0: -                done_que[h][w + 1] = 1 +                distance[h][w - 1] = distance[h][w] + 1 +            if S[h][w + 1] == "." and distance[h][w + 1] == -1: -                distance[h][w + 1] = min(distance[h][w] + 1, distance[h][w + 1]) -            que_i += 1 -        hw_max_n = 0 -        for h2 in range(1, H + 1): -            for w2 in range(1, W + 1): -                if distance[h2][w2] != 1000: -                    hw_max_n = max(hw_max_n, distance[h2][w2]) +                distance[h][w + 1] = distance[h][w] + 1 +        hw_max_n = max([max(dis) for dis in distance])