from collections import deque

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

X = [[1 if a == "#" else 0 for a in eval(input())] for _ in range(H)]



def BFS(i0=0):

    Q = deque([i0])

    D = [-1] * (H * W)

    D[i0] = 0

    while Q:

        x = Q.popleft()

        i, j = x // W, x % W

        for di, dj in ((0, 1), (0, -1), (1, 0), (-1, 0)):

            ni, nj = i + di, j + dj

            if 0 <= ni < H and 0 <= nj < W:

                if X[ni][nj] == 0:

                    y = ni * W + nj

                    if D[y] == -1:

                        D[y] = D[x] + 1

                        Q.append(y)

    return D



ma = 0

for i in range(H):

    for j in range(W):

        if X[i][j] == 0:

            ma = max(ma, max(BFS(i*W+j)))

print(ma)
from collections import deque

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

X = [1] * (W + 2)

for _ in range(H):

    X += [1] + [1 if a == "#" else 0 for a in eval(input())] + [1]

X += [1] * (W + 2)

H, W = H+2, W+2



def BFS(i0=0):

    Q = deque([i0])

    D = [-1] * (H * W)

    D[i0] = 0

    while Q:

        x = Q.popleft()

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

            if X[y] == 0:

                if D[y] == -1:

                    D[y] = D[x] + 1

                    Q.append(y)

    return D



ma = 0

for i in range(H):

    for j in range(W):

        if X[i*W+j] == 0:

            ma = max(ma, max(BFS(i*W+j)))

print(ma)
from collections import deque

H, W = list(map(int, input().split()))
X = [[1 if a == "#" else 0 for a in eval(input())] for _ in range(H)]


def BFS(i0=0):
    Q = deque([i0])
    D = [-1] * (H * W)
    D[i0] = 0
    while Q:
        x = Q.popleft()
        i, j = x // W, x % W
        for di, dj in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W:
                if X[ni][nj] == 0:
                    y = ni * W + nj
                    if D[y] == -1:
                        D[y] = D[x] + 1
                        Q.append(y)
    return D


ma = 0
for i in range(H):
    for j in range(W):
        if X[i][j] == 0:
            ma = max(ma, max(BFS(i * W + j)))
print(ma)

from collections import deque

H, W = list(map(int, input().split()))
X = [1] * (W + 2)
for _ in range(H):
    X += [1] + [1 if a == "#" else 0 for a in eval(input())] + [1]
X += [1] * (W + 2)
H, W = H + 2, W + 2


def BFS(i0=0):
    Q = deque([i0])
    D = [-1] * (H * W)
    D[i0] = 0
    while Q:
        x = Q.popleft()
        for y in (x + W, x - W, x + 1, x - 1):
            if X[y] == 0:
                if D[y] == -1:
                    D[y] = D[x] + 1
                    Q.append(y)
    return D


ma = 0
for i in range(H):
    for j in range(W):
        if X[i * W + j] == 0:
            ma = max(ma, max(BFS(i * W + j)))
print(ma)

-X = [[1 if a == "#" else 0 for a in eval(input())] for _ in range(H)] +X = [1] * (W + 2) +for _ in range(H): +    X += [1] + [1 if a == "#" else 0 for a in eval(input())] + [1] +X += [1] * (W + 2) +H, W = H + 2, W + 2 -        i, j = x // W, x % W -        for di, dj in ((0, 1), (0, -1), (1, 0), (-1, 0)): -            ni, nj = i + di, j + dj -            if 0 <= ni < H and 0 <= nj < W: -                if X[ni][nj] == 0: -                    y = ni * W + nj -                    if D[y] == -1: -                        D[y] = D[x] + 1 -                        Q.append(y) +        for y in (x + W, x - W, x + 1, x - 1): +            if X[y] == 0: +                if D[y] == -1: +                    D[y] = D[x] + 1 +                    Q.append(y) -        if X[i][j] == 0: +        if X[i * W + j] == 0: