from collections import deque

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

maze = [eval(input()) for i in range(H)]



def bfs(maze, sy, sx):

    visited = [[-1]*W for j in range(H)]

    queue = deque([[sy, sx]])

    visited[sy][sx] = 0

    while queue:

        ans=0

        y, x = queue.popleft()

        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            new_y, new_x = y+j, x+k

            if 0 <= new_y < H and 0 <= new_x < W:

                if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1 :

                    visited[new_y][new_x] = visited[y][x] + 1

                    queue.append([new_y, new_x])

                    

    for l in range(H):

        for m in range(W):

            ans = max(ans,visited[l][m])

            

    return ans

        

ma=0

for sy in range(H):

    for sx in range(W):

        if maze[sy][sx] == ".":

            if bfs(maze,sy,sx) is not None:

                ma = max(bfs(maze, sy, sx),ma)

print(ma)

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

C = [list(eval(input())) for i in range(h)]



queue = []

visited = [] 

visited = [[0 for i in range(w)] for i in range(h)]

ans = 0



for i in range(h):

    for j in range(w):

        if C[i][j] == '.':

            queue.append([i,j])

            visited[i][j] = 1



            dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]



            while len(queue) > 0:

                now = queue.pop(0)

                for k in range(4):

                    y = now[0]+dy_dx[k][0]

                    x = now[1]+dy_dx[k][1]

                    if 0 <= y < h and 0 <= x < w:

                        if C[y][x] != '#' and visited[y][x] == 0:

                            visited[y][x] = visited[now[0]][now[1]]+1 #移動回数のカウント

                            queue.append([y,x])

                    

        for l in range(h):

            for m in range(w):

                ans = max(ans,visited[l][m])

            

        queue = []

        visited = [[0 for i in range(w)] for i in range(h)]              

        

print((ans-1))
from collections import deque

H, W = list(map(int, input().split()))
maze = [eval(input()) for i in range(H)]


def bfs(maze, sy, sx):
    visited = [[-1] * W for j in range(H)]
    queue = deque([[sy, sx]])
    visited[sy][sx] = 0
    while queue:
        ans = 0
        y, x = queue.popleft()
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y + j, x + k
            if 0 <= new_y < H and 0 <= new_x < W:
                if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1:
                    visited[new_y][new_x] = visited[y][x] + 1
                    queue.append([new_y, new_x])
    for l in range(H):
        for m in range(W):
            ans = max(ans, visited[l][m])
    return ans


ma = 0
for sy in range(H):
    for sx in range(W):
        if maze[sy][sx] == ".":
            if bfs(maze, sy, sx) is not None:
                ma = max(bfs(maze, sy, sx), ma)
print(ma)

h, w = list(map(int, input().split()))
C = [list(eval(input())) for i in range(h)]
queue = []
visited = []
visited = [[0 for i in range(w)] for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if C[i][j] == ".":
            queue.append([i, j])
            visited[i][j] = 1
            dy_dx = [[1, 0], [0, 1], [-1, 0], [0, -1]]
            while len(queue) > 0:
                now = queue.pop(0)
                for k in range(4):
                    y = now[0] + dy_dx[k][0]
                    x = now[1] + dy_dx[k][1]
                    if 0 <= y < h and 0 <= x < w:
                        if C[y][x] != "#" and visited[y][x] == 0:
                            visited[y][x] = visited[now[0]][now[1]] + 1  # 移動回数のカウント
                            queue.append([y, x])
        for l in range(h):
            for m in range(w):
                ans = max(ans, visited[l][m])
        queue = []
        visited = [[0 for i in range(w)] for i in range(h)]
print((ans - 1))

-from collections import deque - -H, W = list(map(int, input().split())) -maze = [eval(input()) for i in range(H)] - - -def bfs(maze, sy, sx): -    visited = [[-1] * W for j in range(H)] -    queue = deque([[sy, sx]]) -    visited[sy][sx] = 0 -    while queue: -        ans = 0 -        y, x = queue.popleft() -        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]): -            new_y, new_x = y + j, x + k -            if 0 <= new_y < H and 0 <= new_x < W: -                if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1: -                    visited[new_y][new_x] = visited[y][x] + 1 -                    queue.append([new_y, new_x]) -    for l in range(H): -        for m in range(W): -            ans = max(ans, visited[l][m]) -    return ans - - -ma = 0 -for sy in range(H): -    for sx in range(W): -        if maze[sy][sx] == ".": -            if bfs(maze, sy, sx) is not None: -                ma = max(bfs(maze, sy, sx), ma) -print(ma) +h, w = list(map(int, input().split())) +C = [list(eval(input())) for i in range(h)] +queue = [] +visited = [] +visited = [[0 for i in range(w)] for i in range(h)] +ans = 0 +for i in range(h): +    for j in range(w): +        if C[i][j] == ".": +            queue.append([i, j]) +            visited[i][j] = 1 +            dy_dx = [[1, 0], [0, 1], [-1, 0], [0, -1]] +            while len(queue) > 0: +                now = queue.pop(0) +                for k in range(4): +                    y = now[0] + dy_dx[k][0] +                    x = now[1] + dy_dx[k][1] +                    if 0 <= y < h and 0 <= x < w: +                        if C[y][x] != "#" and visited[y][x] == 0: +                            visited[y][x] = visited[now[0]][now[1]] + 1  # 移動回数のカウント +                            queue.append([y, x]) +        for l in range(h): +            for m in range(w): +                ans = max(ans, visited[l][m]) +        queue = [] +        visited = [[0 for i in range(w)] for i in range(h)] +print((ans - 1))