from collections import deque

 

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

S = [eval(input()) for _ in range(H)]

 

INF = 10**15

d = [(1, 0), (-1, 0), (0, 1), (0, -1)]

 

answer = 0

for i in range(H):

  for j in range(W):

    if S[i][j] == "#":

      continue

    else:

      dist = [[INF for _ in range(W)] for _ in range(H)]

      dist[i][j] = 0

      stack = deque([(i, j)])

      

      while stack:

        s = stack.popleft()

        si, sj = s

        for di, dj in d:

          if 0 <= si + di <= H - 1 and 0 <= sj + dj <= W - 1 and S[si + di][sj + dj] != "#":

            if dist[si + di][sj + dj] > dist[si][sj] + 1:

              dist[si + di][sj + dj] = dist[si][sj] + 1

              stack.append((si + di, sj + dj))

      #print(dist)

 

      max_dist = 0

      for ii in range(H):

        #print(dist[ii])

        for jj in range(W):

          if dist[ii][jj] != INF:

            max_dist = max(max_dist, dist[ii][jj])

      #print("")

      answer = max(max_dist, answer)

      

print(answer)
from collections import deque

 

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

S = [eval(input()) for _ in range(H)]



INF = 10**15

graph = [[] for _ in range(H * W)]

 

for i in range(H):

  for j in range(W):

    for ni, nj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:

      if 0 <= ni < H and 0 <= nj < W and S[ni][nj] != "#":

        graph[W * i + j].append((W * ni + nj, 1))



answer = 0

for i in range(H):

  for j in range(W):

    if S[i][j] == "#":

      continue

    else:

      dist = [INF for _ in range(H * W)]

      dist[W * i + j] = 0

      stack = deque([W * i + j])

      while stack:

        s = stack.popleft()

        for g, w in graph[s]:

          if dist[g] > dist[s] + w:

            dist[g] = dist[s] + w

            stack.append(g)

      for ii in range(H * W):

        if dist[ii] != INF:

          answer = max(answer, dist[ii])

      

print(answer)
from collections import deque

H, W = list(map(int, input().split()))
S = [eval(input()) for _ in range(H)]
INF = 10**15
d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
answer = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        else:
            dist = [[INF for _ in range(W)] for _ in range(H)]
            dist[i][j] = 0
            stack = deque([(i, j)])
            while stack:
                s = stack.popleft()
                si, sj = s
                for di, dj in d:
                    if (
                        0 <= si + di <= H - 1
                        and 0 <= sj + dj <= W - 1
                        and S[si + di][sj + dj] != "#"
                    ):
                        if dist[si + di][sj + dj] > dist[si][sj] + 1:
                            dist[si + di][sj + dj] = dist[si][sj] + 1
                            stack.append((si + di, sj + dj))
            # print(dist)
            max_dist = 0
            for ii in range(H):
                # print(dist[ii])
                for jj in range(W):
                    if dist[ii][jj] != INF:
                        max_dist = max(max_dist, dist[ii][jj])
            # print("")
            answer = max(max_dist, answer)
print(answer)

from collections import deque

H, W = list(map(int, input().split()))
S = [eval(input()) for _ in range(H)]
INF = 10**15
graph = [[] for _ in range(H * W)]
for i in range(H):
    for j in range(W):
        for ni, nj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
            if 0 <= ni < H and 0 <= nj < W and S[ni][nj] != "#":
                graph[W * i + j].append((W * ni + nj, 1))
answer = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        else:
            dist = [INF for _ in range(H * W)]
            dist[W * i + j] = 0
            stack = deque([W * i + j])
            while stack:
                s = stack.popleft()
                for g, w in graph[s]:
                    if dist[g] > dist[s] + w:
                        dist[g] = dist[s] + w
                        stack.append(g)
            for ii in range(H * W):
                if dist[ii] != INF:
                    answer = max(answer, dist[ii])
print(answer)

-d = [(1, 0), (-1, 0), (0, 1), (0, -1)] +graph = [[] for _ in range(H * W)] +for i in range(H): +    for j in range(W): +        for ni, nj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]: +            if 0 <= ni < H and 0 <= nj < W and S[ni][nj] != "#": +                graph[W * i + j].append((W * ni + nj, 1)) -            dist = [[INF for _ in range(W)] for _ in range(H)] -            dist[i][j] = 0 -            stack = deque([(i, j)]) +            dist = [INF for _ in range(H * W)] +            dist[W * i + j] = 0 +            stack = deque([W * i + j]) -                si, sj = s -                for di, dj in d: -                    if ( -                        0 <= si + di <= H - 1 -                        and 0 <= sj + dj <= W - 1 -                        and S[si + di][sj + dj] != "#" -                    ): -                        if dist[si + di][sj + dj] > dist[si][sj] + 1: -                            dist[si + di][sj + dj] = dist[si][sj] + 1 -                            stack.append((si + di, sj + dj)) -            # print(dist) -            max_dist = 0 -            for ii in range(H): -                # print(dist[ii]) -                for jj in range(W): -                    if dist[ii][jj] != INF: -                        max_dist = max(max_dist, dist[ii][jj]) -            # print("") -            answer = max(max_dist, answer) +                for g, w in graph[s]: +                    if dist[g] > dist[s] + w: +                        dist[g] = dist[s] + w +                        stack.append(g) +            for ii in range(H * W): +                if dist[ii] != INF: +                    answer = max(answer, dist[ii])