from collections import deque

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

c = ["#"*(W+2)] + ["#" + eval(input()) + "#" for _ in range(H)] + ["#"*(W+2)]





def bfs(start):

	queue = deque([start])

	#visited = [[False for _ in range(W+2)] for _ in range(H+2)]

	tekazu = [[-1 for _ in range(W+2)] for _ in range(H+2)]

	tekazu[start[0]][start[1]] = 0

	num = 0

	while queue:



		label = queue.pop()

		

			#print(visited)

		if c[label[0] + 1][label[1]] == "." and tekazu[label[0] + 1][label[1]] < 0:

			queue.appendleft([label[0] + 1, label[1]])

			tekazu[label[0] + 1][label[1]] = tekazu[label[0]][label[1]] + 1

			num = max(num, tekazu[label[0] + 1][label[1]])



		if c[label[0] - 1][label[1]] == "." and tekazu[label[0] - 1][label[1]] < 0:

			queue.appendleft([label[0] - 1, label[1]])

			tekazu[label[0] - 1][label[1]] = tekazu[label[0]][label[1]] + 1

			num = max(num, tekazu[label[0] - 1][label[1]])



		if c[label[0]][label[1] + 1] == "." and tekazu[label[0]][label[1] + 1] < 0:

			queue.appendleft([label[0], label[1] + 1])

			tekazu[label[0]][label[1] + 1] = tekazu[label[0]][label[1]] + 1

			num = max(num, tekazu[label[0]][label[1] + 1])



		if c[label[0]][label[1] - 1] == "." and tekazu[label[0]][label[1] - 1] < 0:

			queue.appendleft([label[0], label[1] - 1])

			tekazu[label[0]][label[1] - 1] = tekazu[label[0]][label[1]] + 1

			num = max(num, tekazu[label[0]][label[1] - 1])



	return num



ans = 0

for h in range(1, H+1):

	for w in range(1, W+1):

		if c[h][w] == ".":

			ans = max(ans, bfs([h, w]))

print(ans)
from collections import deque

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

s = ["#"*(W+2)] + ["#" + eval(input()) + "#" for _ in range(H)] + ["#"*(W+2)]

DH, DW = [0, 0, 1, -1], [1, -1, 0, 0]



def bfs(start):

	que = deque([start])

	dist = [[-1 for _ in range(W+2)] for _ in range(H+2)]

	dist[start[0]][start[1]] = 0

	num = 0



	while que:



		label = que.pop()

		h, w = label[0], label[1]

		

		for dh, dw in zip(DH, DW):

			hh, ww = h+dh, w+dw

			if s[hh][ww] == "." and dist[hh][ww] < 0:

				que.appendleft((hh, ww))

				dist[hh][ww] = dist[h][w] + 1

				num = max(num, dist[hh][ww])



	return num



ans = 0

for h in range(1, H+1):

	for w in range(1, W+1):

		if s[h][w] == ".":

			ans = max(ans, bfs((h, w)))

print(ans)
from collections import deque

H, W = list(map(int, input().split()))
c = ["#" * (W + 2)] + ["#" + eval(input()) + "#" for _ in range(H)] + ["#" * (W + 2)]


def bfs(start):
    queue = deque([start])
    # visited = [[False for _ in range(W+2)] for _ in range(H+2)]
    tekazu = [[-1 for _ in range(W + 2)] for _ in range(H + 2)]
    tekazu[start[0]][start[1]] = 0
    num = 0
    while queue:
        label = queue.pop()
        # print(visited)
        if c[label[0] + 1][label[1]] == "." and tekazu[label[0] + 1][label[1]] < 0:
            queue.appendleft([label[0] + 1, label[1]])
            tekazu[label[0] + 1][label[1]] = tekazu[label[0]][label[1]] + 1
            num = max(num, tekazu[label[0] + 1][label[1]])
        if c[label[0] - 1][label[1]] == "." and tekazu[label[0] - 1][label[1]] < 0:
            queue.appendleft([label[0] - 1, label[1]])
            tekazu[label[0] - 1][label[1]] = tekazu[label[0]][label[1]] + 1
            num = max(num, tekazu[label[0] - 1][label[1]])
        if c[label[0]][label[1] + 1] == "." and tekazu[label[0]][label[1] + 1] < 0:
            queue.appendleft([label[0], label[1] + 1])
            tekazu[label[0]][label[1] + 1] = tekazu[label[0]][label[1]] + 1
            num = max(num, tekazu[label[0]][label[1] + 1])
        if c[label[0]][label[1] - 1] == "." and tekazu[label[0]][label[1] - 1] < 0:
            queue.appendleft([label[0], label[1] - 1])
            tekazu[label[0]][label[1] - 1] = tekazu[label[0]][label[1]] + 1
            num = max(num, tekazu[label[0]][label[1] - 1])
    return num


ans = 0
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if c[h][w] == ".":
            ans = max(ans, bfs([h, w]))
print(ans)

from collections import deque

H, W = list(map(int, input().split()))
s = ["#" * (W + 2)] + ["#" + eval(input()) + "#" for _ in range(H)] + ["#" * (W + 2)]
DH, DW = [0, 0, 1, -1], [1, -1, 0, 0]


def bfs(start):
    que = deque([start])
    dist = [[-1 for _ in range(W + 2)] for _ in range(H + 2)]
    dist[start[0]][start[1]] = 0
    num = 0
    while que:
        label = que.pop()
        h, w = label[0], label[1]
        for dh, dw in zip(DH, DW):
            hh, ww = h + dh, w + dw
            if s[hh][ww] == "." and dist[hh][ww] < 0:
                que.appendleft((hh, ww))
                dist[hh][ww] = dist[h][w] + 1
                num = max(num, dist[hh][ww])
    return num


ans = 0
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if s[h][w] == ".":
            ans = max(ans, bfs((h, w)))
print(ans)

-c = ["#" * (W + 2)] + ["#" + eval(input()) + "#" for _ in range(H)] + ["#" * (W + 2)] +s = ["#" * (W + 2)] + ["#" + eval(input()) + "#" for _ in range(H)] + ["#" * (W + 2)] +DH, DW = [0, 0, 1, -1], [1, -1, 0, 0] -    queue = deque([start]) -    # visited = [[False for _ in range(W+2)] for _ in range(H+2)] -    tekazu = [[-1 for _ in range(W + 2)] for _ in range(H + 2)] -    tekazu[start[0]][start[1]] = 0 +    que = deque([start]) +    dist = [[-1 for _ in range(W + 2)] for _ in range(H + 2)] +    dist[start[0]][start[1]] = 0 -    while queue: -        label = queue.pop() -        # print(visited) -        if c[label[0] + 1][label[1]] == "." and tekazu[label[0] + 1][label[1]] < 0: -            queue.appendleft([label[0] + 1, label[1]]) -            tekazu[label[0] + 1][label[1]] = tekazu[label[0]][label[1]] + 1 -            num = max(num, tekazu[label[0] + 1][label[1]]) -        if c[label[0] - 1][label[1]] == "." and tekazu[label[0] - 1][label[1]] < 0: -            queue.appendleft([label[0] - 1, label[1]]) -            tekazu[label[0] - 1][label[1]] = tekazu[label[0]][label[1]] + 1 -            num = max(num, tekazu[label[0] - 1][label[1]]) -        if c[label[0]][label[1] + 1] == "." and tekazu[label[0]][label[1] + 1] < 0: -            queue.appendleft([label[0], label[1] + 1]) -            tekazu[label[0]][label[1] + 1] = tekazu[label[0]][label[1]] + 1 -            num = max(num, tekazu[label[0]][label[1] + 1]) -        if c[label[0]][label[1] - 1] == "." and tekazu[label[0]][label[1] - 1] < 0: -            queue.appendleft([label[0], label[1] - 1]) -            tekazu[label[0]][label[1] - 1] = tekazu[label[0]][label[1]] + 1 -            num = max(num, tekazu[label[0]][label[1] - 1]) +    while que: +        label = que.pop() +        h, w = label[0], label[1] +        for dh, dw in zip(DH, DW): +            hh, ww = h + dh, w + dw +            if s[hh][ww] == "." and dist[hh][ww] < 0: +                que.appendleft((hh, ww)) +                dist[hh][ww] = dist[h][w] + 1 +                num = max(num, dist[hh][ww]) -        if c[h][w] == ".": -            ans = max(ans, bfs([h, w])) +        if s[h][w] == ".": +            ans = max(ans, bfs((h, w)))