from collections import deque



def shortestpath(y0, x0):

    step_cnt = [[-1] * w for _ in range(h)]

    step_cnt[y0-1][x0-1] = 0

    q.append((y0, x0))

    while q:

        y, x = q.popleft()

        for i in range(4):

            y_next = y + dy[i]

            x_next = x + dx[i]

            if 1 <= y_next < h+1 and 1 <= x_next < w+1\

            and maze[y_next][x_next] == '.'\

            and step_cnt[y_next-1][x_next-1] == -1:

                q.append((y_next, x_next))

                step_cnt[y_next-1][x_next-1] = step_cnt[y-1][x-1] + 1



    return step_cnt[y-1][x-1]



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

maze = [['#'] * (w+2)]

maze += [['#']+[x for x in input()]+['#'] for _ in range(h)]

maze += [['#'] * (w+2)]

dy = [-1, 0, 1, 0]

dx = [0, -1, 0, 1]

q = deque()



max_steps = -1

for y0 in range(1, h+1):

    for x0 in range(1, w+1):

        if maze[y0][x0] != '#':

            max_steps = max(max_steps, shortestpath(y0, x0))



print(max_steps)

from collections import deque



def shortestpath(y0, x0):

    step_cnt = [[-1] * (w+2) for _ in range(h+2)]

    step_cnt[y0][x0] = 0

    q.append((y0, x0))

    while q:

        y, x = q.popleft()

        for i in range(4):

            y_next = y + dy[i]

            x_next = x + dx[i]

            if maze[y_next][x_next] == '.'\

            and step_cnt[y_next][x_next] == -1:

                q.append((y_next, x_next))

                step_cnt[y_next][x_next] = step_cnt[y][x] + 1



    

    return step_cnt[y][x]



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

maze = [['#'] * (w+2)]

maze += [['#']+[x for x in input()]+['#'] for _ in range(h)]

maze += [['#'] * (w+2)]

dy = [-1, 0, 1, 0]

dx = [0, -1, 0, 1]

q = deque()



max_steps = -1

for y0 in range(1, h+1):

    for x0 in range(1, w+1):

        if maze[y0][x0] != '#':

            max_steps = max(max_steps, shortestpath(y0, x0))



print(max_steps)

from collections import deque


def shortestpath(y0, x0):
    step_cnt = [[-1] * w for _ in range(h)]
    step_cnt[y0 - 1][x0 - 1] = 0
    q.append((y0, x0))
    while q:
        y, x = q.popleft()
        for i in range(4):
            y_next = y + dy[i]
            x_next = x + dx[i]
            if (
                1 <= y_next < h + 1
                and 1 <= x_next < w + 1
                and maze[y_next][x_next] == "."
                and step_cnt[y_next - 1][x_next - 1] == -1
            ):
                q.append((y_next, x_next))
                step_cnt[y_next - 1][x_next - 1] = step_cnt[y - 1][x - 1] + 1
    return step_cnt[y - 1][x - 1]


h, w = map(int, input().split())
maze = [["#"] * (w + 2)]
maze += [["#"] + [x for x in input()] + ["#"] for _ in range(h)]
maze += [["#"] * (w + 2)]
dy = [-1, 0, 1, 0]
dx = [0, -1, 0, 1]
q = deque()
max_steps = -1
for y0 in range(1, h + 1):
    for x0 in range(1, w + 1):
        if maze[y0][x0] != "#":
            max_steps = max(max_steps, shortestpath(y0, x0))
print(max_steps)

from collections import deque


def shortestpath(y0, x0):
    step_cnt = [[-1] * (w + 2) for _ in range(h + 2)]
    step_cnt[y0][x0] = 0
    q.append((y0, x0))
    while q:
        y, x = q.popleft()
        for i in range(4):
            y_next = y + dy[i]
            x_next = x + dx[i]
            if maze[y_next][x_next] == "." and step_cnt[y_next][x_next] == -1:
                q.append((y_next, x_next))
                step_cnt[y_next][x_next] = step_cnt[y][x] + 1
    return step_cnt[y][x]


h, w = map(int, input().split())
maze = [["#"] * (w + 2)]
maze += [["#"] + [x for x in input()] + ["#"] for _ in range(h)]
maze += [["#"] * (w + 2)]
dy = [-1, 0, 1, 0]
dx = [0, -1, 0, 1]
q = deque()
max_steps = -1
for y0 in range(1, h + 1):
    for x0 in range(1, w + 1):
        if maze[y0][x0] != "#":
            max_steps = max(max_steps, shortestpath(y0, x0))
print(max_steps)

-    step_cnt = [[-1] * w for _ in range(h)] -    step_cnt[y0 - 1][x0 - 1] = 0 +    step_cnt = [[-1] * (w + 2) for _ in range(h + 2)] +    step_cnt[y0][x0] = 0 -            if ( -                1 <= y_next < h + 1 -                and 1 <= x_next < w + 1 -                and maze[y_next][x_next] == "." -                and step_cnt[y_next - 1][x_next - 1] == -1 -            ): +            if maze[y_next][x_next] == "." and step_cnt[y_next][x_next] == -1: -                step_cnt[y_next - 1][x_next - 1] = step_cnt[y - 1][x - 1] + 1 -    return step_cnt[y - 1][x - 1] +                step_cnt[y_next][x_next] = step_cnt[y][x] + 1 +    return step_cnt[y][x]