from collections import deque



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

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



def solve(x, y):

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

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

    visited[y][x] = 0

    

    q = deque()

    q.append((x, y))



    while q:

        x, y = q.popleft()



        for u, v in offset:

            if not (0 <= x + u < W) or not (0 <= y + v < H):

                continue



            if visited[y + v][x + u] > -1 or S[y + v][x + u] == "#":

                continue



            visited[y + v][x + u] = visited[y][x] + 1

            q.append((x + u, y + v))

            

    return visited



ans = 0

for y, ss in enumerate(S):

    for x, s in enumerate(ss):

        if s == ".":

            ans = max(ans, max(max(v) for v in solve(x, y)))



print(ans)

from collections import deque



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

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





def solve_maze(s):

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

    inf = 10 ** 9



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

    d[s[0]][s[1]] = 0



    q = deque()

    q.append(s)

    while q:

        u, v = q.popleft()

        for i, j in bias:

            if (0 <= u + i < H and 0 <= v + j < W

                    and X[u + i][v + j] == "."

                    and d[u + i][v + j] > d[u][v] + 1):

                d[u + i][v + j] = d[u][v] + 1

                q.append((u + i, v + j))



    res = 0

    for i in range(H):

        for j in range(W):

            if d[i][j] < inf:

                res = max(res, d[i][j])

                

    return res





ans = 0

for i in range(H):

    for j in range(W):

        if X[i][j] == ".":

            ans = max(ans, solve_maze((i, j)))



print(ans)

from collections import deque

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


def solve(x, y):
    offset = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    visited = [[-1] * W for _ in range(H)]
    visited[y][x] = 0
    q = deque()
    q.append((x, y))
    while q:
        x, y = q.popleft()
        for u, v in offset:
            if not (0 <= x + u < W) or not (0 <= y + v < H):
                continue
            if visited[y + v][x + u] > -1 or S[y + v][x + u] == "#":
                continue
            visited[y + v][x + u] = visited[y][x] + 1
            q.append((x + u, y + v))
    return visited


ans = 0
for y, ss in enumerate(S):
    for x, s in enumerate(ss):
        if s == ".":
            ans = max(ans, max(max(v) for v in solve(x, y)))
print(ans)

from collections import deque

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


def solve_maze(s):
    bias = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    inf = 10**9
    d = [[inf] * W for _ in range(H)]
    d[s[0]][s[1]] = 0
    q = deque()
    q.append(s)
    while q:
        u, v = q.popleft()
        for i, j in bias:
            if (
                0 <= u + i < H
                and 0 <= v + j < W
                and X[u + i][v + j] == "."
                and d[u + i][v + j] > d[u][v] + 1
            ):
                d[u + i][v + j] = d[u][v] + 1
                q.append((u + i, v + j))
    res = 0
    for i in range(H):
        for j in range(W):
            if d[i][j] < inf:
                res = max(res, d[i][j])
    return res


ans = 0
for i in range(H):
    for j in range(W):
        if X[i][j] == ".":
            ans = max(ans, solve_maze((i, j)))
print(ans)

-S = [eval(input()) for _ in range(H)] +X = [eval(input()) for _ in range(H)] -def solve(x, y): -    offset = [(1, 0), (-1, 0), (0, 1), (0, -1)] -    visited = [[-1] * W for _ in range(H)] -    visited[y][x] = 0 +def solve_maze(s): +    bias = [(0, 1), (0, -1), (1, 0), (-1, 0)] +    inf = 10**9 +    d = [[inf] * W for _ in range(H)] +    d[s[0]][s[1]] = 0 -    q.append((x, y)) +    q.append(s) -        x, y = q.popleft() -        for u, v in offset: -            if not (0 <= x + u < W) or not (0 <= y + v < H): -                continue -            if visited[y + v][x + u] > -1 or S[y + v][x + u] == "#": -                continue -            visited[y + v][x + u] = visited[y][x] + 1 -            q.append((x + u, y + v)) -    return visited +        u, v = q.popleft() +        for i, j in bias: +            if ( +                0 <= u + i < H +                and 0 <= v + j < W +                and X[u + i][v + j] == "." +                and d[u + i][v + j] > d[u][v] + 1 +            ): +                d[u + i][v + j] = d[u][v] + 1 +                q.append((u + i, v + j)) +    res = 0 +    for i in range(H): +        for j in range(W): +            if d[i][j] < inf: +                res = max(res, d[i][j]) +    return res -for y, ss in enumerate(S): -    for x, s in enumerate(ss): -        if s == ".": -            ans = max(ans, max(max(v) for v in solve(x, y))) +for i in range(H): +    for j in range(W): +        if X[i][j] == ".": +            ans = max(ans, solve_maze((i, j)))