Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
grid=[None]*H
for i in range(H):
  grid[i]=input()
  
from collections import deque
ans=0
for y1 in range(H):
  for x1 in range(W):
    for y2 in range(H):
      for x2 in range(W):
        if grid[y2][x2]=="#" or grid[y1][x1]=="#":
          continue
        q=deque()
        q.append([y1,x1,0])
        seen=[[False for j in range(W)] for i in range(H)]
        while q:
          node=q.popleft()
          y=node[0]
          x=node[1]
          dist=node[2]
          if seen[y][x]:
            continue
          if grid[y][x]=="#":
            continue
          if y==y2 and x==x2:
            if ans<dist:
              ans=dist
          seen[y][x]=True
          if y-1>=0:
            q.append([y-1,x,dist+1])
          if y+1<H:
            q.append([y+1,x,dist+1])
          if x-1>=0:
            q.append([y,x-1,dist+1])
          if x+1<W:
            q.append([y,x+1,dist+1])
            
print(ans)


Fixed submission:
H,W=map(int,input().split())
grid=[None]*H
for i in range(H):
  grid[i]=input()
  
from collections import deque
ans=0

INF=10**9
ans=0
for i in range(H):
  for j in range(W):
    dist=[[INF for j in range(W)] for i in range(H)]
    q=deque()
    q.append([i,j,0])
    while q:
      node=q.popleft()
      y=node[0]
      x=node[1]
      d=node[2]
      if grid[y][x]=="#":
        continue
      if dist[y][x]<=d:
        continue
      if ans<d:
        ans=d
      dist[y][x]=d
      cases=((0,1),(0,-1),(1,0),(-1,0))
      for c in cases:
        if 0<=y+c[0]<H and 0<=x+c[1]<W:
          q.append([y+c[0],x+c[1],d+1])
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
grid=[None]*H
for i in range(H):
  grid[i]=input()
  
from collections import deque
ans=0
for y1 in range(H):
  for x1 in range(W):
    for y2 in range(H):
      for x2 in range(W):
        if grid[y2][x2]=="#" or grid[y1][x1]=="#":
          continue
        q=deque()
        q.append([y1,x1,0])
        seen=[[False for j in range(W)] for i in range(H)]
        while q:
          node=q.popleft()
          y=node[0]
          x=node[1]
          dist=node[2]
          if seen[y][x]:
            continue
          if grid[y][x]=="#":
            continue
          if y==y2 and x==x2:
            if ans<dist:
              ans=dist
          seen[y][x]=True
          if y-1>=0:
            q.append([y-1,x,dist+1])
          if y+1<H:
            q.append([y+1,x,dist+1])
          if x-1>=0:
            q.append([y,x-1,dist+1])
          if x+1<W:
            q.append([y,x+1,dist+1])
            
print(ans)

Fixed submission:
H,W=map(int,input().split())
grid=[None]*H
for i in range(H):
  grid[i]=input()
  
from collections import deque
ans=0

INF=10**9
ans=0
for i in range(H):
  for j in range(W):
    dist=[[INF for j in range(W)] for i in range(H)]
    q=deque()
    q.append([i,j,0])
    while q:
      node=q.popleft()
      y=node[0]
      x=node[1]
      d=node[2]
      if grid[y][x]=="#":
        continue
      if dist[y][x]<=d:
        continue
      if ans<d:
        ans=d
      dist[y][x]=d
      cases=((0,1),(0,-1),(1,0),(-1,0))
      for c in cases:
        if 0<=y+c[0]<H and 0<=x+c[1]<W:
          q.append([y+c[0],x+c[1],d+1])
      
print(ans)