# D - Maze Master

from collections import deque



MOD = 10 ** 9 + 7

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





def bfs(sx: int, sy: int):

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

    dist = {sx * MOD + sy: 0}

    while queue:

        x, y = queue.popleft()

        for dx, dy in dxy:

            nx, ny = x + dx, y + dy

            if S[nx][ny] == ".":

                if nx * MOD + ny not in dist:

                    dist[nx * MOD + ny] = dist[x * MOD + y] + 1

                    queue.append((nx, ny))

    return max(dist.values())





def main():

    global S

    H, W, *S = open(0).read().split()

    H, W = int(H) + 2, int(W) + 2

    S = ["#" * W] + ["#" + s + "#" for s in S] + ["#" * W]  # put sentinels

    ans = max(bfs(i, j) for i, s in enumerate(S) for j, c in enumerate(s) if c == ".")

    print(ans)





if __name__ == "__main__":

    main()

# D - Maze Master

from collections import deque





def bfs(start: int):

    queue = deque([start])

    dist = {start: 0}

    while queue:

        x = queue.popleft()

        for nx in (x + 1, x - 1, x + W, x - W):

            if maze[nx] and nx not in dist:

                dist[nx] = dist[x] + 1

                queue.append(nx)

    return max(dist.values())





def main():

    global H, W, maze

    H, W, *S = open(0).read().split()

    H, W = int(H) + 2, int(W) + 2

    maze = [0] * W  # flatten S and put sentinels

    for s in S:

        maze += [0] + [c == "." for c in s] + [0]

    maze += [0] * W

    ans = max(bfs(i) for i, x in enumerate(maze) if x)

    print(ans)





if __name__ == "__main__":

    main()

# D - Maze Master
from collections import deque

MOD = 10**9 + 7
dxy = [(-1, 0), (0, -1), (0, 1), (1, 0)]


def bfs(sx: int, sy: int):
    queue = deque([(sx, sy)])
    dist = {sx * MOD + sy: 0}
    while queue:
        x, y = queue.popleft()
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy
            if S[nx][ny] == ".":
                if nx * MOD + ny not in dist:
                    dist[nx * MOD + ny] = dist[x * MOD + y] + 1
                    queue.append((nx, ny))
    return max(dist.values())


def main():
    global S
    H, W, *S = open(0).read().split()
    H, W = int(H) + 2, int(W) + 2
    S = ["#" * W] + ["#" + s + "#" for s in S] + ["#" * W]  # put sentinels
    ans = max(bfs(i, j) for i, s in enumerate(S) for j, c in enumerate(s) if c == ".")
    print(ans)


if __name__ == "__main__":
    main()

# D - Maze Master
from collections import deque


def bfs(start: int):
    queue = deque([start])
    dist = {start: 0}
    while queue:
        x = queue.popleft()
        for nx in (x + 1, x - 1, x + W, x - W):
            if maze[nx] and nx not in dist:
                dist[nx] = dist[x] + 1
                queue.append(nx)
    return max(dist.values())


def main():
    global H, W, maze
    H, W, *S = open(0).read().split()
    H, W = int(H) + 2, int(W) + 2
    maze = [0] * W  # flatten S and put sentinels
    for s in S:
        maze += [0] + [c == "." for c in s] + [0]
    maze += [0] * W
    ans = max(bfs(i) for i, x in enumerate(maze) if x)
    print(ans)


if __name__ == "__main__":
    main()

-MOD = 10**9 + 7 -dxy = [(-1, 0), (0, -1), (0, 1), (1, 0)] - -def bfs(sx: int, sy: int): -    queue = deque([(sx, sy)]) -    dist = {sx * MOD + sy: 0} +def bfs(start: int): +    queue = deque([start]) +    dist = {start: 0} -        x, y = queue.popleft() -        for dx, dy in dxy: -            nx, ny = x + dx, y + dy -            if S[nx][ny] == ".": -                if nx * MOD + ny not in dist: -                    dist[nx * MOD + ny] = dist[x * MOD + y] + 1 -                    queue.append((nx, ny)) +        x = queue.popleft() +        for nx in (x + 1, x - 1, x + W, x - W): +            if maze[nx] and nx not in dist: +                dist[nx] = dist[x] + 1 +                queue.append(nx) -    global S +    global H, W, maze -    S = ["#" * W] + ["#" + s + "#" for s in S] + ["#" * W]  # put sentinels -    ans = max(bfs(i, j) for i, s in enumerate(S) for j, c in enumerate(s) if c == ".") +    maze = [0] * W  # flatten S and put sentinels +    for s in S: +        maze += [0] + [c == "." for c in s] + [0] +    maze += [0] * W +    ans = max(bfs(i) for i, x in enumerate(maze) if x)