Buggy python submission (verdict: Time Limit Exceeded):
def astar(start, goal, S, H, W):
  if S[start[0]][start[1]] == '#' or S[goal[0]][goal[1]] == '#':
    return -1
  else:
    INF = 10**9
    D = [[INF]*W for h in range(H)]
    D[start[0]][start[1]] = 0
    next_node = [start]
    while next_node:
      now_y, now_x = next_node.pop()
      d = D[now_y][now_x]
      for x, y in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        if now_y+y<0 or now_y+y>=H or now_x+x<0 or now_x+x>=W:
          continue
        if S[now_y+y][now_x+x] == '.':
          if D[now_y+y][now_x+x] > d + 1:
            D[now_y+y][now_x+x] = D[now_y][now_x] + 1
            next_node.append([now_y+y, now_x+x])
    
    if D[goal[0]][goal[1]] < INF:
      return D[goal[0]][goal[1]]
    else:
      return -1


H, W = map(int, input().split())
nodes = []
for h in range(H):
  for w in range(W):
    nodes.append([h, w])

S = []
for h in range(H):
  S.append(list(input()))

ans = 0
num_nodes = len(nodes)
for i in range(num_nodes):
  for j in range(i+1, num_nodes):
    ans = max(ans, astar(nodes[i], nodes[j], S, H, W))

print(ans)

Fixed submission:
from collections import deque

def astar(start, S, H, W):
  if S[start[0]][start[1]] == '#':
    return -1
  else:
    INF = 10**9
    D = [[INF]*W for h in range(H)]
    D[start[0]][start[1]] = 0
    next_node = deque([start])
    max_d = 0
    while next_node:
      now_y, now_x = next_node.popleft()
      d = D[now_y][now_x]
      for x, y in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        if now_y+y<0 or now_y+y>=H or now_x+x<0 or now_x+x>=W:
          continue
        if S[now_y+y][now_x+x] == '.':
          if D[now_y+y][now_x+x] > d + 1:
            D[now_y+y][now_x+x] = D[now_y][now_x] + 1
            max_d = max(max_d, D[now_y+y][now_x+x])
            next_node.append([now_y+y, now_x+x])
    
    return max_d

H, W = map(int, input().split())
nodes = []
for h in range(H):
  for w in range(W):
    nodes.append([h, w])

S = []
for h in range(H):
  S.append(list(input()))

ans = 0
for start in nodes:
  ans = max(ans, astar(start, S, H, W))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def astar(start, goal, S, H, W):
  if S[start[0]][start[1]] == '#' or S[goal[0]][goal[1]] == '#':
    return -1
  else:
    INF = 10**9
    D = [[INF]*W for h in range(H)]
    D[start[0]][start[1]] = 0
    next_node = [start]
    while next_node:
      now_y, now_x = next_node.pop()
      d = D[now_y][now_x]
      for x, y in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        if now_y+y<0 or now_y+y>=H or now_x+x<0 or now_x+x>=W:
          continue
        if S[now_y+y][now_x+x] == '.':
          if D[now_y+y][now_x+x] > d + 1:
            D[now_y+y][now_x+x] = D[now_y][now_x] + 1
            next_node.append([now_y+y, now_x+x])
    
    if D[goal[0]][goal[1]] < INF:
      return D[goal[0]][goal[1]]
    else:
      return -1


H, W = map(int, input().split())
nodes = []
for h in range(H):
  for w in range(W):
    nodes.append([h, w])

S = []
for h in range(H):
  S.append(list(input()))

ans = 0
for start in nodes:
  for goal in nodes:
    ans = max(ans, astar(start, goal, S, H, W))

print(ans)

Fixed submission:
from collections import deque

def astar(start, S, H, W):
  if S[start[0]][start[1]] == '#':
    return -1
  else:
    INF = 10**9
    D = [[INF]*W for h in range(H)]
    D[start[0]][start[1]] = 0
    next_node = deque([start])
    max_d = 0
    while next_node:
      now_y, now_x = next_node.popleft()
      d = D[now_y][now_x]
      for x, y in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        if now_y+y<0 or now_y+y>=H or now_x+x<0 or now_x+x>=W:
          continue
        if S[now_y+y][now_x+x] == '.':
          if D[now_y+y][now_x+x] > d + 1:
            D[now_y+y][now_x+x] = D[now_y][now_x] + 1
            max_d = max(max_d, D[now_y+y][now_x+x])
            next_node.append([now_y+y, now_x+x])
    
    return max_d

H, W = map(int, input().split())
nodes = []
for h in range(H):
  for w in range(W):
    nodes.append([h, w])

S = []
for h in range(H):
  S.append(list(input()))

ans = 0
for start in nodes:
  ans = max(ans, astar(start, S, H, W))

print(ans)