Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
l=[list(map(str,input())) for _ in range(h)]  
def maze(MAZE,sx,sy):
  dmap=[[h*w+1]*w for _ in range(h)]
  dmap[sx][sy]=0
  cand=[]
  cand.append([sx,sy])
  while len(cand)!=0:
    x,y=cand.pop()
    for next_y,next_x in [(y+1, x), (y, x+1), (y, x-1), (y-1, x)]:
      if 0<=next_x<=h-1 and 0<=next_y<=w-1 and MAZE[next_x][next_y]=='.':
        if dmap[x][y]+1<dmap[next_x][next_y]:
          dmap[next_x][next_y]=dmap[x][y]+1
          cand.append([next_x,next_y])
  return max([dmap[i][j] for i in range(h) for j in range(w) if dmap[i][j]<=h*w])
c=0
for ix in range(h):
  for iy in range(w):
    if l[ix][iy]!='#':
      d=maze(l,ix,iy)
      if c<d:
        c=d
print(c)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
l=[list(map(str,input())) for _ in range(h)]  
def maze(MAZE,sx,sy):
  dmap=[[h*w]*w for _ in range(h)]
  dmap[sx][sy]=0
  cand=deque()
  cand.append([sx,sy])
  while len(cand)!=0:
    x,y=cand.popleft()
    for next_y,next_x in [(y+1, x), (y, x+1), (y, x-1), (y-1, x)]:
      if 0<=next_x<=h-1 and 0<=next_y<=w-1 and MAZE[next_x][next_y]=='.' and dmap[next_x][next_y]==h*w:
        dmap[next_x][next_y]=min(dmap[x][y]+1,dmap[next_x][next_y])
        cand.append([next_x,next_y])
  return max([dmap[i][j] for i in range(h) for j in range(w) if dmap[i][j]<h*w])
c=0
for ix in range(h):
  for iy in range(w):
    if l[ix][iy]!='#':
      d=maze(l,ix,iy)
      if c<d:
        c=d
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
l=[list(map(str,input())) for _ in range(h)]  
def maze(MAZE,sx,sy,gx,gy):
  dmap=[[h*w]*w for _ in range(h)]
  dmap[sx][sy]=0
  dist=h*w
  cand=[]
  visited=set()
  if sx==gx and sy==gy:
    dist=0
  else:
    cand.append([sx,sy])
  while len(cand)!=0:
    x,y=cand.pop()
    visited.add((x,y))
    for next_y,next_x in [(y+1, x), (y, x+1), (y, x-1), (y-1, x)]:
      if next_x==gx and next_y==gy:
        if dist>dmap[x][y]+1:
          dist=dmap[x][y]+1
        break
      elif 0<=next_x<=h-1 and 0<=next_y<=w-1 and MAZE[next_x][next_y]=='.' and not (next_x,next_y) in visited:
        cand.append([next_x,next_y])
        dmap[next_x][next_y]=min(dmap[x][y]+1,dmap[next_x][next_y])
  return dist
c=0
for ix in range(h):
  for iy in range(w):
    if l[ix][iy]!='#':
      for jx in range(h):
        for jy in range(w):
          if l[jx][jy]!='#':
            d=maze(l,ix,iy,jx,jy)
            if c<d:
              c=d
print(c)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
l=[list(map(str,input())) for _ in range(h)]  
def maze(MAZE,sx,sy):
  dmap=[[h*w]*w for _ in range(h)]
  dmap[sx][sy]=0
  cand=deque()
  cand.append([sx,sy])
  while len(cand)!=0:
    x,y=cand.popleft()
    for next_y,next_x in [(y+1, x), (y, x+1), (y, x-1), (y-1, x)]:
      if 0<=next_x<=h-1 and 0<=next_y<=w-1 and MAZE[next_x][next_y]=='.' and dmap[next_x][next_y]==h*w:
        dmap[next_x][next_y]=min(dmap[x][y]+1,dmap[next_x][next_y])
        cand.append([next_x,next_y])
  return max([dmap[i][j] for i in range(h) for j in range(w) if dmap[i][j]<h*w])
c=0
for ix in range(h):
  for iy in range(w):
    if l[ix][iy]!='#':
      d=maze(l,ix,iy)
      if c<d:
        c=d
print(c)