import heapq

INFTY = 10**3

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

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

dist = {(i,j):{(k,l):INFTY for k in range(H) for l in range(W)} for j in range(W) for i in range(H)}

for i in range(H):

    for j in range(W):

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

            hist = {(k,l):0 for k in range(H) for l in range(W)}

            heap = [(0,i,j)]

            dist[(i,j)][(i,j)]= 0

            hist[(i,j)] = 1

            while heap:

                d,k,l = heapq.heappop(heap)

                if d>dist[(i,j)][(k,l)]:continue

                hist[(k,l)] = 1

                if l+1<W and S[k][l+1]=="." and hist[(k,l+1)]==0:

                    if dist[(i,j)][(k,l+1)]>d+1:

                        dist[(i,j)][(k,l+1)] = d+1

                        heapq.heappush(heap,(d+1,k,l+1))

                if l-1>-1 and S[k][l-1]=="." and hist[(k,l-1)]==0:

                    if dist[(i,j)][(k,l-1)]>d+1:

                        dist[(i,j)][(k,l-1)]=d+1

                        heapq.heappush(heap,(d+1,k,l-1))

                if k+1<H and S[k+1][l]=="." and hist[(k+1,l)]==0:

                    if dist[(i,j)][(k+1,l)]>d+1:

                        dist[(i,j)][(k+1,l)]=d+1

                        heapq.heappush(heap,(d+1,k+1,l))

                if k-1>-1 and S[k-1][l]=="." and hist[(k-1,l)]==0:

                    if dist[(i,j)][(k-1,l)]>d+1:

                        dist[(i,j)][(k-1,l)]=d+1

                        heapq.heappush(heap,(d+1,k-1,l))

dmax = 0

for i in range(H):

    for j in range(W):

        for k in range(H):

            for l in range(W):

                if dist[(i,j)][(k,l)]<INFTY:

                    dmax = max(dmax,dist[(i,j)][(k,l)])

print(dmax)
from collections import deque

INFTY = 10**3

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

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

cmax = 0

for x in range(H):

    for y in range(W):

        if S[x][y]==".":

            A = [[INFTY for _ in range(W)] for _ in range(H)]

            hist = {(i,j):0 for i in range(H) for j in range(W)}

            que = deque([(x,y,0)])

            A[x][y] = 0

            hist[(x,y)] = 1

            while que:

                i,j,d = que.popleft()

                if j+1<W and S[i][j+1]=="." and hist[(i,j+1)]==0:

                    A[i][j+1]=d+1

                    hist[(i,j+1)]=1

                    que.append((i,j+1,d+1))

                if i-1>=0 and S[i-1][j]=="." and hist[(i-1,j)]==0:

                    A[i-1][j]=d+1

                    hist[(i-1,j)]=1

                    que.append((i-1,j,d+1))

                if j-1>=0 and S[i][j-1]=="." and hist[(i,j-1)]==0:

                    A[i][j-1]=d+1

                    hist[(i,j-1)]=1

                    que.append((i,j-1,d+1))

                if i+1<H and S[i+1][j]=="." and hist[(i+1,j)]==0:

                    A[i+1][j]=d+1

                    hist[(i+1,j)]=1

                    que.append((i+1,j,d+1))

            cnt=0

            for i in range(H):

                for j in range(W):

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

                        cnt = max(cnt,A[i][j])

            cmax = max(cmax,cnt)

print(cmax)
import heapq

INFTY = 10**3
H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
dist = {
    (i, j): {(k, l): INFTY for k in range(H) for l in range(W)}
    for j in range(W)
    for i in range(H)
}
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            hist = {(k, l): 0 for k in range(H) for l in range(W)}
            heap = [(0, i, j)]
            dist[(i, j)][(i, j)] = 0
            hist[(i, j)] = 1
            while heap:
                d, k, l = heapq.heappop(heap)
                if d > dist[(i, j)][(k, l)]:
                    continue
                hist[(k, l)] = 1
                if l + 1 < W and S[k][l + 1] == "." and hist[(k, l + 1)] == 0:
                    if dist[(i, j)][(k, l + 1)] > d + 1:
                        dist[(i, j)][(k, l + 1)] = d + 1
                        heapq.heappush(heap, (d + 1, k, l + 1))
                if l - 1 > -1 and S[k][l - 1] == "." and hist[(k, l - 1)] == 0:
                    if dist[(i, j)][(k, l - 1)] > d + 1:
                        dist[(i, j)][(k, l - 1)] = d + 1
                        heapq.heappush(heap, (d + 1, k, l - 1))
                if k + 1 < H and S[k + 1][l] == "." and hist[(k + 1, l)] == 0:
                    if dist[(i, j)][(k + 1, l)] > d + 1:
                        dist[(i, j)][(k + 1, l)] = d + 1
                        heapq.heappush(heap, (d + 1, k + 1, l))
                if k - 1 > -1 and S[k - 1][l] == "." and hist[(k - 1, l)] == 0:
                    if dist[(i, j)][(k - 1, l)] > d + 1:
                        dist[(i, j)][(k - 1, l)] = d + 1
                        heapq.heappush(heap, (d + 1, k - 1, l))
dmax = 0
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if dist[(i, j)][(k, l)] < INFTY:
                    dmax = max(dmax, dist[(i, j)][(k, l)])
print(dmax)

from collections import deque

INFTY = 10**3
H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
cmax = 0
for x in range(H):
    for y in range(W):
        if S[x][y] == ".":
            A = [[INFTY for _ in range(W)] for _ in range(H)]
            hist = {(i, j): 0 for i in range(H) for j in range(W)}
            que = deque([(x, y, 0)])
            A[x][y] = 0
            hist[(x, y)] = 1
            while que:
                i, j, d = que.popleft()
                if j + 1 < W and S[i][j + 1] == "." and hist[(i, j + 1)] == 0:
                    A[i][j + 1] = d + 1
                    hist[(i, j + 1)] = 1
                    que.append((i, j + 1, d + 1))
                if i - 1 >= 0 and S[i - 1][j] == "." and hist[(i - 1, j)] == 0:
                    A[i - 1][j] = d + 1
                    hist[(i - 1, j)] = 1
                    que.append((i - 1, j, d + 1))
                if j - 1 >= 0 and S[i][j - 1] == "." and hist[(i, j - 1)] == 0:
                    A[i][j - 1] = d + 1
                    hist[(i, j - 1)] = 1
                    que.append((i, j - 1, d + 1))
                if i + 1 < H and S[i + 1][j] == "." and hist[(i + 1, j)] == 0:
                    A[i + 1][j] = d + 1
                    hist[(i + 1, j)] = 1
                    que.append((i + 1, j, d + 1))
            cnt = 0
            for i in range(H):
                for j in range(W):
                    if S[i][j] == ".":
                        cnt = max(cnt, A[i][j])
            cmax = max(cmax, cnt)
print(cmax)

-import heapq +from collections import deque -dist = { -    (i, j): {(k, l): INFTY for k in range(H) for l in range(W)} -    for j in range(W) -    for i in range(H) -} -for i in range(H): -    for j in range(W): -        if S[i][j] == ".": -            hist = {(k, l): 0 for k in range(H) for l in range(W)} -            heap = [(0, i, j)] -            dist[(i, j)][(i, j)] = 0 -            hist[(i, j)] = 1 -            while heap: -                d, k, l = heapq.heappop(heap) -                if d > dist[(i, j)][(k, l)]: -                    continue -                hist[(k, l)] = 1 -                if l + 1 < W and S[k][l + 1] == "." and hist[(k, l + 1)] == 0: -                    if dist[(i, j)][(k, l + 1)] > d + 1: -                        dist[(i, j)][(k, l + 1)] = d + 1 -                        heapq.heappush(heap, (d + 1, k, l + 1)) -                if l - 1 > -1 and S[k][l - 1] == "." and hist[(k, l - 1)] == 0: -                    if dist[(i, j)][(k, l - 1)] > d + 1: -                        dist[(i, j)][(k, l - 1)] = d + 1 -                        heapq.heappush(heap, (d + 1, k, l - 1)) -                if k + 1 < H and S[k + 1][l] == "." and hist[(k + 1, l)] == 0: -                    if dist[(i, j)][(k + 1, l)] > d + 1: -                        dist[(i, j)][(k + 1, l)] = d + 1 -                        heapq.heappush(heap, (d + 1, k + 1, l)) -                if k - 1 > -1 and S[k - 1][l] == "." and hist[(k - 1, l)] == 0: -                    if dist[(i, j)][(k - 1, l)] > d + 1: -                        dist[(i, j)][(k - 1, l)] = d + 1 -                        heapq.heappush(heap, (d + 1, k - 1, l)) -dmax = 0 -for i in range(H): -    for j in range(W): -        for k in range(H): -            for l in range(W): -                if dist[(i, j)][(k, l)] < INFTY: -                    dmax = max(dmax, dist[(i, j)][(k, l)]) -print(dmax) +cmax = 0 +for x in range(H): +    for y in range(W): +        if S[x][y] == ".": +            A = [[INFTY for _ in range(W)] for _ in range(H)] +            hist = {(i, j): 0 for i in range(H) for j in range(W)} +            que = deque([(x, y, 0)]) +            A[x][y] = 0 +            hist[(x, y)] = 1 +            while que: +                i, j, d = que.popleft() +                if j + 1 < W and S[i][j + 1] == "." and hist[(i, j + 1)] == 0: +                    A[i][j + 1] = d + 1 +                    hist[(i, j + 1)] = 1 +                    que.append((i, j + 1, d + 1)) +                if i - 1 >= 0 and S[i - 1][j] == "." and hist[(i - 1, j)] == 0: +                    A[i - 1][j] = d + 1 +                    hist[(i - 1, j)] = 1 +                    que.append((i - 1, j, d + 1)) +                if j - 1 >= 0 and S[i][j - 1] == "." and hist[(i, j - 1)] == 0: +                    A[i][j - 1] = d + 1 +                    hist[(i, j - 1)] = 1 +                    que.append((i, j - 1, d + 1)) +                if i + 1 < H and S[i + 1][j] == "." and hist[(i + 1, j)] == 0: +                    A[i + 1][j] = d + 1 +                    hist[(i + 1, j)] = 1 +                    que.append((i + 1, j, d + 1)) +            cnt = 0 +            for i in range(H): +                for j in range(W): +                    if S[i][j] == ".": +                        cnt = max(cnt, A[i][j]) +            cmax = max(cmax, cnt) +print(cmax)