import numpy as np

from collections import deque

from itertools import product



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

S = np.full((H, W), 'x', dtype='U1')

for i in range(H):

    S[i] = list(eval(input()))



def bfs(x, y):

    dist = np.full((H, W), -1, dtype='i8')

    que = deque()



    dist[x, y] = 0

    que.append((x, y))



    while que:

        h, w = que.popleft()

        d = dist[h, w]

        for dx, dy in ((0, -1), (-1, 0), (1, 0), (0, 1)):

            hh, ww = h + dx, w + dy

            if hh < 0 or H <= hh or ww < 0 or W <= ww or S[hh, ww] == '#' or dist[hh, ww] != -1:

                continue

            dist[hh, ww] = d + 1

            que.append((hh, ww))



    return d



res = 0

for i, k in product(list(range(H)), list(range(W))):

    if S[i, k] == '#':

        continue

    d = bfs(i, k)

    if d > res:

        res = d

print(res)

import numpy as np

from scipy.sparse.csgraph import floyd_warshall

from itertools import product



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

s = np.full((h, w), 'x', dtype='U1')

for i in range(h):

    s[i] = list(eval(input()))



d = np.identity(h * w, dtype='i1')

for i, k in product(list(range(h)), list(range(w))):

    if s[i, k] == '#':

        continue

    for dy, dx in ((-1, 0), (0, -1), (0, 1), (1, 0)):

        y, x = i + dy, k + dx

        if y < 0 or h <= y or x < 0 or w <= x or s[y, x] == '#':

            continue

        d[i*w+k, y*w+x] = 1

        d[y*w+x, i*w+k] = 1



print((floyd_warshall(d).astype('i8').max()))

import numpy as np
from collections import deque
from itertools import product

H, W = list(map(int, input().split()))
S = np.full((H, W), "x", dtype="U1")
for i in range(H):
    S[i] = list(eval(input()))


def bfs(x, y):
    dist = np.full((H, W), -1, dtype="i8")
    que = deque()
    dist[x, y] = 0
    que.append((x, y))
    while que:
        h, w = que.popleft()
        d = dist[h, w]
        for dx, dy in ((0, -1), (-1, 0), (1, 0), (0, 1)):
            hh, ww = h + dx, w + dy
            if (
                hh < 0
                or H <= hh
                or ww < 0
                or W <= ww
                or S[hh, ww] == "#"
                or dist[hh, ww] != -1
            ):
                continue
            dist[hh, ww] = d + 1
            que.append((hh, ww))
    return d


res = 0
for i, k in product(list(range(H)), list(range(W))):
    if S[i, k] == "#":
        continue
    d = bfs(i, k)
    if d > res:
        res = d
print(res)

import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from itertools import product

h, w = list(map(int, input().split()))
s = np.full((h, w), "x", dtype="U1")
for i in range(h):
    s[i] = list(eval(input()))
d = np.identity(h * w, dtype="i1")
for i, k in product(list(range(h)), list(range(w))):
    if s[i, k] == "#":
        continue
    for dy, dx in ((-1, 0), (0, -1), (0, 1), (1, 0)):
        y, x = i + dy, k + dx
        if y < 0 or h <= y or x < 0 or w <= x or s[y, x] == "#":
            continue
        d[i * w + k, y * w + x] = 1
        d[y * w + x, i * w + k] = 1
print((floyd_warshall(d).astype("i8").max()))

-from collections import deque +from scipy.sparse.csgraph import floyd_warshall -H, W = list(map(int, input().split())) -S = np.full((H, W), "x", dtype="U1") -for i in range(H): -    S[i] = list(eval(input())) - - -def bfs(x, y): -    dist = np.full((H, W), -1, dtype="i8") -    que = deque() -    dist[x, y] = 0 -    que.append((x, y)) -    while que: -        h, w = que.popleft() -        d = dist[h, w] -        for dx, dy in ((0, -1), (-1, 0), (1, 0), (0, 1)): -            hh, ww = h + dx, w + dy -            if ( -                hh < 0 -                or H <= hh -                or ww < 0 -                or W <= ww -                or S[hh, ww] == "#" -                or dist[hh, ww] != -1 -            ): -                continue -            dist[hh, ww] = d + 1 -            que.append((hh, ww)) -    return d - - -res = 0 -for i, k in product(list(range(H)), list(range(W))): -    if S[i, k] == "#": +h, w = list(map(int, input().split())) +s = np.full((h, w), "x", dtype="U1") +for i in range(h): +    s[i] = list(eval(input())) +d = np.identity(h * w, dtype="i1") +for i, k in product(list(range(h)), list(range(w))): +    if s[i, k] == "#": -    d = bfs(i, k) -    if d > res: -        res = d -print(res) +    for dy, dx in ((-1, 0), (0, -1), (0, 1), (1, 0)): +        y, x = i + dy, k + dx +        if y < 0 or h <= y or x < 0 or w <= x or s[y, x] == "#": +            continue +        d[i * w + k, y * w + x] = 1 +        d[y * w + x, i * w + k] = 1 +print((floyd_warshall(d).astype("i8").max()))