import heapq

 

 

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

meiro = []

for i in range(h):

    line = eval(input())

    meiro.append(line)

 

 

def dfs(y, x):

    dist = [[-1]*w for _ in range(h)]

    if meiro[y][x] == '#':

        return 0

    dist[y][x] = 0

 

    pattern = [[-1, 0], [1, 0], [0, -1], [0, 1]]

    todo = []

    for dy, dx in pattern:

        ny, nx = y + dy, x + dx

        if 0 <= ny <= h-1 and 0 <= nx <= w-1:

            if meiro[ny][nx] == '.':

                heapq.heappush(todo, [1, ny, nx])

 

    while todo:

        d, y, x = heapq.heappop(todo)

        if meiro[y][x] == '#':

            continue

        if dist[y][x] != -1:

                continue

 

        dist[y][x] = d

        for dy, dx in pattern:

            ny, nx = y + dy, x + dx

            if 0 <= ny <= h - 1 and 0 <= nx <= w - 1:

                if dist[ny][nx] == -1 and meiro[ny][nx] == '.':

                    heapq.heappush(todo, [d+1, ny, nx])

 

    res = 0

    for line in dist:

        res = max(max(line), res)

 

    return res

 

 

ans = []

for y in range(h):

    for x in range(w):

        ans.append(dfs(y, x))

 

print((max(ans)))
from collections import deque





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

meiro = []

for i in range(h):

    line = eval(input())

    meiro.append(line)





def bfs(y, x):

    dist = [[-1]*w for _ in range(h)]

    if meiro[y][x] == '#':

        return 0

    dist[y][x] = 0



    pattern = [[-1, 0], [1, 0], [0, -1], [0, 1]]

    todo = deque([])

    for dy, dx in pattern:

        ny, nx = y + dy, x + dx

        if 0 <= ny <= h-1 and 0 <= nx <= w-1:

            todo.append([ny, nx, 1])



    while todo:

        y, x, d = todo.popleft()

        if meiro[y][x] == '#':

            continue

        if dist[y][x] != -1:

            continue



        dist[y][x] = d

        for dy, dx in pattern:

            ny, nx = y + dy, x + dx

            if 0 <= ny <= h - 1 and 0 <= nx <= w - 1 and dist[ny][nx] == -1:

                todo.append([ny, nx, d+1])



    res = 0

    for line in dist:

        res = max(max(line), res)



    return res





ans = []

for y in range(h):

    for x in range(w):

        ans.append(bfs(y, x))



print((max(ans)))

import heapq

h, w = list(map(int, input().split()))
meiro = []
for i in range(h):
    line = eval(input())
    meiro.append(line)


def dfs(y, x):
    dist = [[-1] * w for _ in range(h)]
    if meiro[y][x] == "#":
        return 0
    dist[y][x] = 0
    pattern = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    todo = []
    for dy, dx in pattern:
        ny, nx = y + dy, x + dx
        if 0 <= ny <= h - 1 and 0 <= nx <= w - 1:
            if meiro[ny][nx] == ".":
                heapq.heappush(todo, [1, ny, nx])
    while todo:
        d, y, x = heapq.heappop(todo)
        if meiro[y][x] == "#":
            continue
        if dist[y][x] != -1:
            continue
        dist[y][x] = d
        for dy, dx in pattern:
            ny, nx = y + dy, x + dx
            if 0 <= ny <= h - 1 and 0 <= nx <= w - 1:
                if dist[ny][nx] == -1 and meiro[ny][nx] == ".":
                    heapq.heappush(todo, [d + 1, ny, nx])
    res = 0
    for line in dist:
        res = max(max(line), res)
    return res


ans = []
for y in range(h):
    for x in range(w):
        ans.append(dfs(y, x))
print((max(ans)))

from collections import deque

h, w = list(map(int, input().split()))
meiro = []
for i in range(h):
    line = eval(input())
    meiro.append(line)


def bfs(y, x):
    dist = [[-1] * w for _ in range(h)]
    if meiro[y][x] == "#":
        return 0
    dist[y][x] = 0
    pattern = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    todo = deque([])
    for dy, dx in pattern:
        ny, nx = y + dy, x + dx
        if 0 <= ny <= h - 1 and 0 <= nx <= w - 1:
            todo.append([ny, nx, 1])
    while todo:
        y, x, d = todo.popleft()
        if meiro[y][x] == "#":
            continue
        if dist[y][x] != -1:
            continue
        dist[y][x] = d
        for dy, dx in pattern:
            ny, nx = y + dy, x + dx
            if 0 <= ny <= h - 1 and 0 <= nx <= w - 1 and dist[ny][nx] == -1:
                todo.append([ny, nx, d + 1])
    res = 0
    for line in dist:
        res = max(max(line), res)
    return res


ans = []
for y in range(h):
    for x in range(w):
        ans.append(bfs(y, x))
print((max(ans)))

-import heapq +from collections import deque -def dfs(y, x): +def bfs(y, x): -    todo = [] +    todo = deque([]) -            if meiro[ny][nx] == ".": -                heapq.heappush(todo, [1, ny, nx]) +            todo.append([ny, nx, 1]) -        d, y, x = heapq.heappop(todo) +        y, x, d = todo.popleft() -            if 0 <= ny <= h - 1 and 0 <= nx <= w - 1: -                if dist[ny][nx] == -1 and meiro[ny][nx] == ".": -                    heapq.heappush(todo, [d + 1, ny, nx]) +            if 0 <= ny <= h - 1 and 0 <= nx <= w - 1 and dist[ny][nx] == -1: +                todo.append([ny, nx, d + 1]) -        ans.append(dfs(y, x)) +        ans.append(bfs(y, x))