Buggy python submission (verdict: Time Limit Exceeded):
def main():
  from collections import deque
  H, W = map(int, input().split())
  graph = [['#'] * (W+2) for _ in range(H+2)]
  for i in range(H):
    S = input()
    for j in range(W):
      graph[i+1][j+1] = S[j]

  def dfs(sx, sy, gx, gy):
    q = deque([(sx, sy)])
    dist = [[-1] * (W+2) for _ in range(H+2)]
    dist[sx][sy] = 0
    while q:
      x, y = q.popleft()
      dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
      for dx, dy in dxy:
        nextx = x+dx
        nexty = y+dy
        if(dist[nextx][nexty] >= 0):
          continue
        if(graph[nextx][nexty] == '#'):
          continue
        dist[nextx][nexty] = dist[x][y] + 1
        q.append((nextx, nexty))
    return dist[gx][gy]

  ans = 0  
  for sx in range(1, H+1):
    for sy in range(1, W+1):
      for gx in range(1, H+1):
        for gy in range(1, W+1):
          if(graph[sx][sy] == '.' and graph[gx][gy] == '.'):
            tmp = dfs(sx, sy, gx, gy)
            ans = max(ans, tmp)
          
  print(ans)
  
main()

Fixed submission:
def main():
  from collections import deque
  H, W = map(int, input().split())
  graph = [['#'] * (W+2) for _ in range(H+2)]
  for i in range(H):
    S = input()
    for j in range(W):
      graph[i+1][j+1] = S[j]

  def dfs(sx, sy):
    q = deque([(sx, sy)])
    dist = [[-1] * (W+2) for _ in range(H+2)]
    dist[sx][sy] = 0
    s = 0
    while q:
      x, y = q.popleft()
      dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
      for dx, dy in dxy:
        nextx = x+dx
        nexty = y+dy
        if(dist[nextx][nexty] >= 0):
          continue
        if(graph[nextx][nexty] == '#'):
          continue
        dist[nextx][nexty] = dist[x][y] + 1
        q.append((nextx, nexty))
    for i in range(H+2):
      for j in range(W+2):
        if(dist[i][j] >= 0):
          s = max(s, dist[i][j])
    return s

  ans = 0  
  for sx in range(1, H+1):
    for sy in range(1, W+1):
      if(graph[sx][sy] == '.'):
          tmp = dfs(sx, sy)
          ans = max(ans, tmp)
          
  print(ans)
  
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
graph = [['#'] * (W+2) for _ in range(H+2)]
for i in range(H):
  S = input()
  for j in range(W):
    graph[i+1][j+1] = S[j]

def dfs(sx, sy, gx, gy):
  q = deque([(sx, sy)])
  dist = [[-1] * (W+2) for _ in range(H+2)]
  dist[sx][sy] = 0
  while q:
    x, y = q.popleft()
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for dx, dy in dxy:
      nextx = x+dx
      nexty = y+dy
      if(dist[nextx][nexty] >= 0):
        continue
      if(graph[nextx][nexty] == '#'):
        continue
      dist[nextx][nexty] = dist[x][y] + 1
      q.append((nextx, nexty))
  return dist[gx][gy]

ans = 0  
for sx in range(1, H+1):
  for sy in range(1, W+1):
    for gx in range(1, H+1):
      for gy in range(1, W+1):
        if(graph[sx][sy] == '.' and graph[gx][gy] == '.'):
          tmp = dfs(sx, sy, gx, gy)
          ans = max(ans, tmp)
          
print(ans)

Fixed submission:
def main():
  from collections import deque
  H, W = map(int, input().split())
  graph = [['#'] * (W+2) for _ in range(H+2)]
  for i in range(H):
    S = input()
    for j in range(W):
      graph[i+1][j+1] = S[j]

  def dfs(sx, sy):
    q = deque([(sx, sy)])
    dist = [[-1] * (W+2) for _ in range(H+2)]
    dist[sx][sy] = 0
    s = 0
    while q:
      x, y = q.popleft()
      dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
      for dx, dy in dxy:
        nextx = x+dx
        nexty = y+dy
        if(dist[nextx][nexty] >= 0):
          continue
        if(graph[nextx][nexty] == '#'):
          continue
        dist[nextx][nexty] = dist[x][y] + 1
        q.append((nextx, nexty))
    for i in range(H+2):
      for j in range(W+2):
        if(dist[i][j] >= 0):
          s = max(s, dist[i][j])
    return s

  ans = 0  
  for sx in range(1, H+1):
    for sy in range(1, W+1):
      if(graph[sx][sy] == '.'):
          tmp = dfs(sx, sy)
          ans = max(ans, tmp)
          
  print(ans)
  
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
X = [input() for _ in range(H)]
maxloc = -10

def bfs(sx, sy, gx, gy):
  dist = [[-1]*W for _ in range(H)]
  dist[sx-1][sy-1] = 0
  q = deque([(sx-1, sy-1)])
  while q:
    u, v = q.popleft()
    for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
      if not(0 <= u+i < H and 0 <= v+j < W):
        continue
    
      if(dist[u+i][v+j] >= 0 or X[u+i][v+j] == "#"):
        continue

      dist[u+i][v+j] = dist[u][v]+1
      q.append((u+i, v+j))

  return dist[gx-1][gy-1]

for i in range(H):
  for j in range(W):
    sx = i
    sy = j
    for k in range(H):
      for l in range(W):
        gx = k
        gy = l
        if(bfs(i+1, j+1, k+1, l+1) > maxloc):
          maxloc = bfs(i+1, j+1, k+1, l+1)
          
print(maxloc)

Fixed submission:
def main():
  from collections import deque
  H, W = map(int, input().split())
  graph = [['#'] * (W+2) for _ in range(H+2)]
  for i in range(H):
    S = input()
    for j in range(W):
      graph[i+1][j+1] = S[j]

  def dfs(sx, sy):
    q = deque([(sx, sy)])
    dist = [[-1] * (W+2) for _ in range(H+2)]
    dist[sx][sy] = 0
    s = 0
    while q:
      x, y = q.popleft()
      dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
      for dx, dy in dxy:
        nextx = x+dx
        nexty = y+dy
        if(dist[nextx][nexty] >= 0):
          continue
        if(graph[nextx][nexty] == '#'):
          continue
        dist[nextx][nexty] = dist[x][y] + 1
        q.append((nextx, nexty))
    for i in range(H+2):
      for j in range(W+2):
        if(dist[i][j] >= 0):
          s = max(s, dist[i][j])
    return s

  ans = 0  
  for sx in range(1, H+1):
    for sy in range(1, W+1):
      if(graph[sx][sy] == '.'):
          tmp = dfs(sx, sy)
          ans = max(ans, tmp)
          
  print(ans)
  
main()

Buggy python submission (verdict: Runtime Error):
from collections import deque
import math
maxloc = -10
h, w = map(int, input().split())
maze = [list(input()) for i in range(h)]
for y in range(h):
    for x in range(w):
        if maze[y][x]=='.':
            maze[y][x]=1000000

for i in range(h):
  for j in range(w):
    sy = i
    sx = j
    if(maze[sy-1][sx-1] != '#'):
      maze[sy-1][sx-1]=0
      for k in range(h):
        for l in range(w):
          gy = k
          gx = l
          if(maze[gy-1][gx-1] != '#'):
            que = deque([[sy-1, sx-1]])
            while que:
              y, x = que.popleft()
              for i, j in [(1,0),(0,1),(-1,0),(0,-1)]:
                nexty, nextx = y+i, x+j
                dist = maze[nexty][nextx]
                if(dist != '#'):
                  if dist > maze[y][x]+1:
                    maze[nexty][nextx] = maze[y][x]+1
                    que.append([nexty, nextx])
            if(int(maze[gy-1][gx-1])>maxloc):
              maxloc = maze[gy-1][gx-1]
            
print(maxloc)

Fixed submission:
def main():
  from collections import deque
  H, W = map(int, input().split())
  graph = [['#'] * (W+2) for _ in range(H+2)]
  for i in range(H):
    S = input()
    for j in range(W):
      graph[i+1][j+1] = S[j]

  def dfs(sx, sy):
    q = deque([(sx, sy)])
    dist = [[-1] * (W+2) for _ in range(H+2)]
    dist[sx][sy] = 0
    s = 0
    while q:
      x, y = q.popleft()
      dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
      for dx, dy in dxy:
        nextx = x+dx
        nexty = y+dy
        if(dist[nextx][nexty] >= 0):
          continue
        if(graph[nextx][nexty] == '#'):
          continue
        dist[nextx][nexty] = dist[x][y] + 1
        q.append((nextx, nexty))
    for i in range(H+2):
      for j in range(W+2):
        if(dist[i][j] >= 0):
          s = max(s, dist[i][j])
    return s

  ans = 0  
  for sx in range(1, H+1):
    for sy in range(1, W+1):
      if(graph[sx][sy] == '.'):
          tmp = dfs(sx, sy)
          ans = max(ans, tmp)
          
  print(ans)
  
main()