from collections import deque

def bfs(loc):

  global ans

  d=[[float('inf')]*w for i in range(h)]

  dx=[1,0,-1,0]

  dy=[0,1,0,-1]

  que=deque([])

  que.append(loc)

  d[loc[0]][loc[1]]=0

  while que:

    p=que.popleft()

    for i in range(4):

      nx=p[0]+dx[i]

      ny=p[1]+dy[i]

      if 0<=nx<h and 0<=ny<w and maze[nx][ny]=='.' and d[nx][ny]==float('inf'):

        d[nx][ny]=d[p[0]][p[1]]+1

        que.append([nx,ny])

        if ans<d[nx][ny]:ans=d[nx][ny]

  return ans



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

maze=[list(eval(input())) for i in range(h)]

a=0

ans=0

loc=[]

for i in range(h):

  for j in range(w):

    if maze[i][j]!='#':

      loc=[i,j]

    else:continue

    a=max(a,bfs(loc))

print(a)
from collections import deque

def bfs(yy,xx):

  global ans

  d=[[float('inf')]*w for i in range(h)]

  dx=[1,0,-1,0]

  dy=[0,1,0,-1]

  q=deque([])

  q.append((yy,xx))

  d[yy][xx]=0

  while q:

    y,x=q.popleft()

    for i in range(4):

      ny,nx=y+dy[i],x+dx[i]

      if 0<=ny<h and 0<=nx<w and s[ny][nx]=='.' and d[ny][nx]==float('inf'):

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

        q.append((ny,nx))

        if ans<d[ny][nx]:

          ans=d[ny][nx]

  return ans

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

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

a=0

ans=0

for i in range(h):

  for j in range(w):

    if s[i][j]=='.':

      a=max(a,bfs(i,j))

print(a)
from collections import deque


def bfs(loc):
    global ans
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([])
    que.append(loc)
    d[loc[0]][loc[1]] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < h
                and 0 <= ny < w
                and maze[nx][ny] == "."
                and d[nx][ny] == float("inf")
            ):
                d[nx][ny] = d[p[0]][p[1]] + 1
                que.append([nx, ny])
                if ans < d[nx][ny]:
                    ans = d[nx][ny]
    return ans


h, w = list(map(int, input().split()))
maze = [list(eval(input())) for i in range(h)]
a = 0
ans = 0
loc = []
for i in range(h):
    for j in range(w):
        if maze[i][j] != "#":
            loc = [i, j]
        else:
            continue
        a = max(a, bfs(loc))
print(a)

from collections import deque


def bfs(yy, xx):
    global ans
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    q = deque([])
    q.append((yy, xx))
    d[yy][xx] = 0
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny, nx = y + dy[i], x + dx[i]
            if (
                0 <= ny < h
                and 0 <= nx < w
                and s[ny][nx] == "."
                and d[ny][nx] == float("inf")
            ):
                d[ny][nx] = d[y][x] + 1
                q.append((ny, nx))
                if ans < d[ny][nx]:
                    ans = d[ny][nx]
    return ans


h, w = list(map(int, input().split()))
s = [list(eval(input())) for i in range(h)]
a = 0
ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            a = max(a, bfs(i, j))
print(a)

-def bfs(loc): +def bfs(yy, xx): -    que = deque([]) -    que.append(loc) -    d[loc[0]][loc[1]] = 0 -    while que: -        p = que.popleft() +    q = deque([]) +    q.append((yy, xx)) +    d[yy][xx] = 0 +    while q: +        y, x = q.popleft() -            nx = p[0] + dx[i] -            ny = p[1] + dy[i] +            ny, nx = y + dy[i], x + dx[i] -                0 <= nx < h -                and 0 <= ny < w -                and maze[nx][ny] == "." -                and d[nx][ny] == float("inf") +                0 <= ny < h +                and 0 <= nx < w +                and s[ny][nx] == "." +                and d[ny][nx] == float("inf") -                d[nx][ny] = d[p[0]][p[1]] + 1 -                que.append([nx, ny]) -                if ans < d[nx][ny]: -                    ans = d[nx][ny] +                d[ny][nx] = d[y][x] + 1 +                q.append((ny, nx)) +                if ans < d[ny][nx]: +                    ans = d[ny][nx] -maze = [list(eval(input())) for i in range(h)] +s = [list(eval(input())) for i in range(h)] -loc = [] -        if maze[i][j] != "#": -            loc = [i, j] -        else: -            continue -        a = max(a, bfs(loc)) +        if s[i][j] == ".": +            a = max(a, bfs(i, j))