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

S = [eval(input()) for i in range(h)]



import numpy as np

from scipy.sparse.csgraph import floyd_warshall



m = h*w



N = [[float("inf")]* m for i in range(m)]



for i in range(m):

    N[i][i] = 0



for i in range(h):

    for j in range(w):

        t=w*i+j

        if S[i][j] != "#":

            for dt in [[-1,0],[1,0],[0,1],[0,-1]]:

                di = dt[0]

                dj=dt[1]

                ni = i+di

                nj = j+dj

                if 0 <= ni < h and 0 <= nj < w and S[ni][nj] == ".":

                    N[t][w*ni+nj] = 1



# print(N)



# for k in range(m):

#     for i in range(m):

#         for j in range(m):

#             N[i][j] = min(N[i][j], N[i][k]+N[k][j])



N = floyd_warshall(N)





for i in range(m):

    for j in range(m):

        if N[i][j] == float("inf"):

            N[i][j] = -1

ans = 0

# print(N)

for i in range(m):

    ans = max(ans, max(N[i]))

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

S = [eval(input()) for i in range(h)]



ans = -1



for si in range(h):

    for sj in range(w):

        if S[si][sj] == ".":

            V = [[-1] * w for i in range(h)]

            Q = [[si, sj, 0]]

            s = 0

            V[si][sj] = 0

            while s < len(Q):

                i = Q[s][0]

                j = Q[s][1]

                d = Q[s][2]

                for t in [[-1,0], [1,0], [0,-1], [0,1]]:

                    di = t[0]

                    dj = t[1]

                    if 0 <= i+di < h and 0 <= j+dj < w and V[i+di][j+dj] == -1 and S[i+di][j+dj] == ".":

                        V[i+di][j+dj] = d+1

                        Q.append([i+di, j+dj, d+1])

                s += 1



            # print(V)



            ans = max(ans, max([max(V[i]) for i in range(h)]))

print(ans)
h, w = list(map(int, input().split()))
S = [eval(input()) for i in range(h)]
import numpy as np
from scipy.sparse.csgraph import floyd_warshall

m = h * w
N = [[float("inf")] * m for i in range(m)]
for i in range(m):
    N[i][i] = 0
for i in range(h):
    for j in range(w):
        t = w * i + j
        if S[i][j] != "#":
            for dt in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
                di = dt[0]
                dj = dt[1]
                ni = i + di
                nj = j + dj
                if 0 <= ni < h and 0 <= nj < w and S[ni][nj] == ".":
                    N[t][w * ni + nj] = 1
# print(N)
# for k in range(m):
#     for i in range(m):
#         for j in range(m):
#             N[i][j] = min(N[i][j], N[i][k]+N[k][j])
N = floyd_warshall(N)
for i in range(m):
    for j in range(m):
        if N[i][j] == float("inf"):
            N[i][j] = -1
ans = 0
# print(N)
for i in range(m):
    ans = max(ans, max(N[i]))
print((int(ans)))

h, w = list(map(int, input().split()))
S = [eval(input()) for i in range(h)]
ans = -1
for si in range(h):
    for sj in range(w):
        if S[si][sj] == ".":
            V = [[-1] * w for i in range(h)]
            Q = [[si, sj, 0]]
            s = 0
            V[si][sj] = 0
            while s < len(Q):
                i = Q[s][0]
                j = Q[s][1]
                d = Q[s][2]
                for t in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
                    di = t[0]
                    dj = t[1]
                    if (
                        0 <= i + di < h
                        and 0 <= j + dj < w
                        and V[i + di][j + dj] == -1
                        and S[i + di][j + dj] == "."
                    ):
                        V[i + di][j + dj] = d + 1
                        Q.append([i + di, j + dj, d + 1])
                s += 1
            # print(V)
            ans = max(ans, max([max(V[i]) for i in range(h)]))
print(ans)

-import numpy as np -from scipy.sparse.csgraph import floyd_warshall - -m = h * w -N = [[float("inf")] * m for i in range(m)] -for i in range(m): -    N[i][i] = 0 -for i in range(h): -    for j in range(w): -        t = w * i + j -        if S[i][j] != "#": -            for dt in [[-1, 0], [1, 0], [0, 1], [0, -1]]: -                di = dt[0] -                dj = dt[1] -                ni = i + di -                nj = j + dj -                if 0 <= ni < h and 0 <= nj < w and S[ni][nj] == ".": -                    N[t][w * ni + nj] = 1 -# print(N) -# for k in range(m): -#     for i in range(m): -#         for j in range(m): -#             N[i][j] = min(N[i][j], N[i][k]+N[k][j]) -N = floyd_warshall(N) -for i in range(m): -    for j in range(m): -        if N[i][j] == float("inf"): -            N[i][j] = -1 -ans = 0 -# print(N) -for i in range(m): -    ans = max(ans, max(N[i])) -print((int(ans))) +ans = -1 +for si in range(h): +    for sj in range(w): +        if S[si][sj] == ".": +            V = [[-1] * w for i in range(h)] +            Q = [[si, sj, 0]] +            s = 0 +            V[si][sj] = 0 +            while s < len(Q): +                i = Q[s][0] +                j = Q[s][1] +                d = Q[s][2] +                for t in [[-1, 0], [1, 0], [0, -1], [0, 1]]: +                    di = t[0] +                    dj = t[1] +                    if ( +                        0 <= i + di < h +                        and 0 <= j + dj < w +                        and V[i + di][j + dj] == -1 +                        and S[i + di][j + dj] == "." +                    ): +                        V[i + di][j + dj] = d + 1 +                        Q.append([i + di, j + dj, d + 1]) +                s += 1 +            # print(V) +            ans = max(ans, max([max(V[i]) for i in range(h)])) +print(ans)