https://www.acmicpc.net/problem/17086
import sys
sys.setrecursionlimit(10**6)
from collections import deque
dx=[0,0,1,-1,1,1,-1,-1]
dy=[1,-1,0,0,1,-1,-1,1]
def bfs(a,b):
check=[[False for _ in range(m+1)] for _ in range(n+1)]
q=deque()
q.append((a,b,0))
check[a][b]=True
while len(q)>0:
v0, v1, v2=list(q.popleft())
for i in range(8):
x=v0+dx[i]
y=v1+dy[i]
z=v2+1
if x<0 or x>=n or y<0 or y>=m or check[x][y]==True: continue
if arr[x][y]==1: return z
q.append((x,y,z))
check[x][y]=True
return 0
n,m=map(int,input().split())
arr=[[0 for _ in range(m+1)] for _ in range(n+1)]
for i in range(n):
st=list(map(int,input().split()))
for j in range(m):
arr[i][j]=st[j]
num=[]
for i in range(n):
for j in range(m):
if arr[i][j]!=1:
num.append(bfs(i,j))
print(max(num))