from collections import deque



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

s = [list(str(eval(input()))) for _ in range(h)]





def bfs(h, w, s, s_h, s_w):

    if s[s_h][s_w] == '#':

        return 0

    d_max = 0

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

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

    dist[s_h][s_w], pas[s_h][s_w] = 0, 's'

    que = deque()

    que.append((s_h, s_w))

    while len(que) > 0:

        q = que.popleft()

        near = [(q[0]+1, q[1]), (q[0], q[1]+1), (q[0]-1, q[1]), (q[0], q[1]-1)]

        for i in near:

            if (i[0] < 0) | (i[0] >= h) | (i[1] < 0) | (i[1] >= w):

                continue

            if (dist[i[0]][i[1]] == -1) & (s[i[0]][i[1]] == '.'):

                dist[i[0]][i[1]] = dist[q[0]][q[1]] + 1

                pas[i[0]][i[1]] = q

                que.append(i)

                d_max = max(dist[q[0]][q[1]] + 1, d_max)

    return d_max





dmax = 0

for i in range(h):

    for j in range(w):

        dist = bfs(h, w, s, i, j)

        dmax = max(dmax, dist)

print(dmax)

from collections import deque





def bfs(y, x):

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

    dist[y][x] = 0

    que, frag = deque([(x, y)]), set([(x, y)])



    while que:

        qx, qy = que.popleft()

        near = [(qx - 1, qy), (qx, qy - 1), (qx + 1, qy), (qx, qy + 1)]

        for i, j in near:

            if (i, j) in frag:

                continue

            if 0 <= i < w and 0 <= j < h and s[j][i] == '.':

                dist[j][i] = dist[qy][qx] + 1

                que.append((i, j)), frag.add((i, j))

    return dist[qy][qx]





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

s = [eval(input()) for _ in range(h)]



ans = 0

for i, si in enumerate(s):

    for j, sij in enumerate(si):

        if sij == '.':

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

print(ans)

from collections import deque

h, w = list(map(int, input().split()))
s = [list(str(eval(input()))) for _ in range(h)]


def bfs(h, w, s, s_h, s_w):
    if s[s_h][s_w] == "#":
        return 0
    d_max = 0
    pas = [[-1 for _ in range(w)] for _ in range(h)]
    dist = [[-1 for _ in range(w)] for _ in range(h)]
    dist[s_h][s_w], pas[s_h][s_w] = 0, "s"
    que = deque()
    que.append((s_h, s_w))
    while len(que) > 0:
        q = que.popleft()
        near = [(q[0] + 1, q[1]), (q[0], q[1] + 1), (q[0] - 1, q[1]), (q[0], q[1] - 1)]
        for i in near:
            if (i[0] < 0) | (i[0] >= h) | (i[1] < 0) | (i[1] >= w):
                continue
            if (dist[i[0]][i[1]] == -1) & (s[i[0]][i[1]] == "."):
                dist[i[0]][i[1]] = dist[q[0]][q[1]] + 1
                pas[i[0]][i[1]] = q
                que.append(i)
                d_max = max(dist[q[0]][q[1]] + 1, d_max)
    return d_max


dmax = 0
for i in range(h):
    for j in range(w):
        dist = bfs(h, w, s, i, j)
        dmax = max(dmax, dist)
print(dmax)

from collections import deque


def bfs(y, x):
    dist = [[-1] * w for _ in range(h)]
    dist[y][x] = 0
    que, frag = deque([(x, y)]), set([(x, y)])
    while que:
        qx, qy = que.popleft()
        near = [(qx - 1, qy), (qx, qy - 1), (qx + 1, qy), (qx, qy + 1)]
        for i, j in near:
            if (i, j) in frag:
                continue
            if 0 <= i < w and 0 <= j < h and s[j][i] == ".":
                dist[j][i] = dist[qy][qx] + 1
                que.append((i, j)), frag.add((i, j))
    return dist[qy][qx]


h, w = list(map(int, input().split()))
s = [eval(input()) for _ in range(h)]
ans = 0
for i, si in enumerate(s):
    for j, sij in enumerate(si):
        if sij == ".":
            ans = max(ans, bfs(i, j))
print(ans)

-h, w = list(map(int, input().split())) -s = [list(str(eval(input()))) for _ in range(h)] + +def bfs(y, x): +    dist = [[-1] * w for _ in range(h)] +    dist[y][x] = 0 +    que, frag = deque([(x, y)]), set([(x, y)]) +    while que: +        qx, qy = que.popleft() +        near = [(qx - 1, qy), (qx, qy - 1), (qx + 1, qy), (qx, qy + 1)] +        for i, j in near: +            if (i, j) in frag: +                continue +            if 0 <= i < w and 0 <= j < h and s[j][i] == ".": +                dist[j][i] = dist[qy][qx] + 1 +                que.append((i, j)), frag.add((i, j)) +    return dist[qy][qx] -def bfs(h, w, s, s_h, s_w): -    if s[s_h][s_w] == "#": -        return 0 -    d_max = 0 -    pas = [[-1 for _ in range(w)] for _ in range(h)] -    dist = [[-1 for _ in range(w)] for _ in range(h)] -    dist[s_h][s_w], pas[s_h][s_w] = 0, "s" -    que = deque() -    que.append((s_h, s_w)) -    while len(que) > 0: -        q = que.popleft() -        near = [(q[0] + 1, q[1]), (q[0], q[1] + 1), (q[0] - 1, q[1]), (q[0], q[1] - 1)] -        for i in near: -            if (i[0] < 0) | (i[0] >= h) | (i[1] < 0) | (i[1] >= w): -                continue -            if (dist[i[0]][i[1]] == -1) & (s[i[0]][i[1]] == "."): -                dist[i[0]][i[1]] = dist[q[0]][q[1]] + 1 -                pas[i[0]][i[1]] = q -                que.append(i) -                d_max = max(dist[q[0]][q[1]] + 1, d_max) -    return d_max - - -dmax = 0 -for i in range(h): -    for j in range(w): -        dist = bfs(h, w, s, i, j) -        dmax = max(dmax, dist) -print(dmax) +h, w = list(map(int, input().split())) +s = [eval(input()) for _ in range(h)] +ans = 0 +for i, si in enumerate(s): +    for j, sij in enumerate(si): +        if sij == ".": +            ans = max(ans, bfs(i, j)) +print(ans)