Buggy python submission (verdict: Runtime Error):
from collections import deque

h, w = map(int, input().split())
c = [list(input()) for i in range(h)]
ans = -1

def bfs(sx, sy):
    que = deque([[sy, sx]])
    d = [["."] * w for _ in range(h)]
    count = 0
    d[sy][sx] = 0
    while que:
        y, x = que.popleft()
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            nx, ny = x+i, y+j
            if 0 <= nx and nx < w and 0 <= ny and ny < h and d[ny][nx] == "." and c[ny][nx] != "#":
                nx_, ny_ = nx-i, ny-j
                d[ny][nx] = d[ny_][nx_] + 1
                que.append([ny, nx])
    return max(max(d))

for i_ in range(h):
    for j_ in range(w):
        if c[i_][j_] == "#":
            continue
        ans = max(ans, bfs( j_, i_))

print(ans)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
c = [input() for _ in range(h)]
ans = -1

def bfs(sx, sy):
    que = deque([[sy, sx]])
    d = [[-1] * w for _ in range(h)]
    d[sy][sx] = 0
    res = 0
    while que:
        y, x = que.popleft()
        res = max(res, d[y][x])
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            nx, ny = x+i, y+j
            if 0 <= nx and nx < w and 0 <= ny and ny < h and d[ny][nx] == -1 and c[ny][nx] != "#":
                nx_, ny_ = nx-i, ny-j
                d[ny][nx] = d[ny_][nx_] + 1
                que.append([ny, nx])
    return res

for i_ in range(h):
    for j_ in range(w):
        if c[i_][j_] != "#":
            ans = max(ans, bfs( j_, i_))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def bfs():
    d = [[float("inf")]* m for i in range(n)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    sx = a1
    sy = a2
    gx = a3
    gy = a4
    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0
    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gx][gy]
n, m = map(int, input().split())
maze = [list(input()) for i in range(n)]
ans = 0
for a1 in range(n):
    for a2 in range(m):
        if maze[a1][a2] == ".":
            maze[a1][a2]  = "S"
            for a3 in range(n):
                for a4 in range(m):
                     if maze[a3][a4] == ".":
                        maze[a3][a4]  = "G"
                        ans = max(bfs(),ans)
                        maze[a3][a4]  = "."
            maze[a1][a2]  = "."
print(ans)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
c = [input() for _ in range(h)]
ans = -1

def bfs(sx, sy):
    que = deque([[sy, sx]])
    d = [[-1] * w for _ in range(h)]
    d[sy][sx] = 0
    res = 0
    while que:
        y, x = que.popleft()
        res = max(res, d[y][x])
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            nx, ny = x+i, y+j
            if 0 <= nx and nx < w and 0 <= ny and ny < h and d[ny][nx] == -1 and c[ny][nx] != "#":
                nx_, ny_ = nx-i, ny-j
                d[ny][nx] = d[ny_][nx_] + 1
                que.append([ny, nx])
    return res

for i_ in range(h):
    for j_ in range(w):
        if c[i_][j_] != "#":
            ans = max(ans, bfs( j_, i_))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def bfs():
    d = [[float("inf")]* m for i in range(n)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    for i in range(n):
        for j in range(m):
            if maze[i][j] == "S":
                sx = i
                sy = j
            if maze[i][j] == "G":
                gx = i
                gy = j
    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0
    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gx][gy]
n, m = map(int, input().split())
maze = [list(input()) for i in range(n)]
ans = 0
for a1 in range(n):
    for a2 in range(m):
        if maze[a1][a2] == ".":
            maze[a1][a2]  = "S"
            for a3 in range(n):
                for a4 in range(m):
                     if maze[a3][a4] == ".":
                        maze[a3][a4]  = "G"
                        ans = max(bfs(),ans)
                        maze[a3][a4]  = "."
            maze[a1][a2]  = "."
print(ans)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
c = [input() for _ in range(h)]
ans = -1

def bfs(sx, sy):
    que = deque([[sy, sx]])
    d = [[-1] * w for _ in range(h)]
    d[sy][sx] = 0
    res = 0
    while que:
        y, x = que.popleft()
        res = max(res, d[y][x])
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            nx, ny = x+i, y+j
            if 0 <= nx and nx < w and 0 <= ny and ny < h and d[ny][nx] == -1 and c[ny][nx] != "#":
                nx_, ny_ = nx-i, ny-j
                d[ny][nx] = d[ny_][nx_] + 1
                que.append([ny, nx])
    return res

for i_ in range(h):
    for j_ in range(w):
        if c[i_][j_] != "#":
            ans = max(ans, bfs( j_, i_))

print(ans)