def solve():
    def bfs(maze, start, distances):
        from collections import deque

        directions = ((-1,0), (1,0), (0,-1), (0, 1))
        H = len(maze)
        W = len(maze[0])
        nexts = deque()
        nexts.append((start, 0))
        x,y = start
        maze[x][y] = 0
        while nexts:
            cur_pos,cur_distance = nexts.popleft()
            x,y = cur_pos
            is_deadend = True
            for direction in directions:
                dx,dy = direction
                next_x = x + dx
                next_y = y + dy
                next_pos = (next_x, next_y)
                if 0 <= next_x < H and 0 <= next_y < W and next_pos:
                    next_symbol = maze[next_x][next_y]
                    if next_symbol == '.':
                        maze[next_x][next_y] = cur_distance + 1
                        nexts.append(((next_x, next_y), cur_distance+1))
                        is_deadend = False
                    elif next_symbol == '#':
                        continue
                    elif next_symbol > cur_distance:
                        is_deadend = False
            if is_deadend:
                distances[cur_pos] = cur_distance
            cur_distance += 1

    H,W = [int(i) for i in input().split()]
    # print(H,W)
    maze = []
    for i in range(H):
        row = [c for c in input()]
        maze.append(row)

    distances = {}
    for i in range(H):
        for j in range(W):
            if maze[i][j] == '.':
                start = (i,j)
                distances[start] = 0
                bfs(maze, start, distances)
                break

    sorted_distances = sorted(distances.values())
    # print(distances)
    # print(sorted_distances)
    print(sorted_distances[-1] + sorted_distances[-2])


if __name__ == "__main__":
    solve()