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

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

def warshall_floyd(d):

    #d[i][j]: iからjへの最短距離

    for k in range(n):

        for i in range(n):

            for j in range(n):

                d[i][j] = min(d[i][j],d[i][k] + d[k][j])

    return d

n=h*w

INF=1<<20

d = [[float("inf") for i in range(n)] for i in range(n)]

#d[u][v] : 辺uvのコスト(存在しないときはinf)

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

for i in range(h):

    for j in range(w):

        if s[i][j]=="#":

            continue

        for dy,dx in dyx:

            if 0<=i+dy<h and 0<=j+dx<w and s[i+dy][j+dx]==".":

                d[i*w+j][(i+dy)*w+j+dx]=1

                d[(i+dy)*w+j+dx][i*w+j]=1







for i in range(n):

    d[i][i] = 0 #自身のところに行くコストは０



x=warshall_floyd(d)

for i in range(n):

    for j in range(n):

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

            x[i][j]=-1



ans=0

for i in range(n):

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

print(ans)

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

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

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

from collections import deque

q=deque()

ans=0

for i in range(h):

    for j in range(w):

        if s[i][j]=="#":

            continue

        q.append((i,j))

        d=[[0]*w for _ in range(h)]

        while q:

            y,x=q.popleft()

            for dy,dx in dyx:

                ny,nx=y+dy,x+dx

                if ny==i and nx==j:

                    continue

                if 0<=ny<h and 0<=nx<w and d[ny][nx]==0 and s[ny][nx]==".":

                    d[ny][nx]=d[y][x]+1

                    q.append((ny,nx))

        for k in range(h):

            ans=max(ans,max(d[k]))

print(ans)

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


def warshall_floyd(d):
    # d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


n = h * w
INF = 1 << 20
d = [[float("inf") for i in range(n)] for i in range(n)]
# d[u][v] : 辺uvのコスト(存在しないときはinf)
dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        for dy, dx in dyx:
            if 0 <= i + dy < h and 0 <= j + dx < w and s[i + dy][j + dx] == ".":
                d[i * w + j][(i + dy) * w + j + dx] = 1
                d[(i + dy) * w + j + dx][i * w + j] = 1
for i in range(n):
    d[i][i] = 0  # 自身のところに行くコストは０
x = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if x[i][j] == float("inf"):
            x[i][j] = -1
ans = 0
for i in range(n):
    ans = max(ans, max(x[i]))
print(ans)

h, w = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(h)]
dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)]
from collections import deque

q = deque()
ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        q.append((i, j))
        d = [[0] * w for _ in range(h)]
        while q:
            y, x = q.popleft()
            for dy, dx in dyx:
                ny, nx = y + dy, x + dx
                if ny == i and nx == j:
                    continue
                if 0 <= ny < h and 0 <= nx < w and d[ny][nx] == 0 and s[ny][nx] == ".":
                    d[ny][nx] = d[y][x] + 1
                    q.append((ny, nx))
        for k in range(h):
            ans = max(ans, max(d[k]))
print(ans)

+dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)] +from collections import deque - -def warshall_floyd(d): -    # d[i][j]: iからjへの最短距離 -    for k in range(n): -        for i in range(n): -            for j in range(n): -                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) -    return d - - -n = h * w -INF = 1 << 20 -d = [[float("inf") for i in range(n)] for i in range(n)] -# d[u][v] : 辺uvのコスト(存在しないときはinf) -dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)] +q = deque() +ans = 0 -        for dy, dx in dyx: -            if 0 <= i + dy < h and 0 <= j + dx < w and s[i + dy][j + dx] == ".": -                d[i * w + j][(i + dy) * w + j + dx] = 1 -                d[(i + dy) * w + j + dx][i * w + j] = 1 -for i in range(n): -    d[i][i] = 0  # 自身のところに行くコストは０ -x = warshall_floyd(d) -for i in range(n): -    for j in range(n): -        if x[i][j] == float("inf"): -            x[i][j] = -1 -ans = 0 -for i in range(n): -    ans = max(ans, max(x[i])) +        q.append((i, j)) +        d = [[0] * w for _ in range(h)] +        while q: +            y, x = q.popleft() +            for dy, dx in dyx: +                ny, nx = y + dy, x + dx +                if ny == i and nx == j: +                    continue +                if 0 <= ny < h and 0 <= nx < w and d[ny][nx] == 0 and s[ny][nx] == ".": +                    d[ny][nx] = d[y][x] + 1 +                    q.append((ny, nx)) +        for k in range(h): +            ans = max(ans, max(d[k]))