Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
H, W = map(int, input().split())
 
init_field = [list(input()) for _ in range(H)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
 
def bfs(x, y):
    distance = [[-1]*W for _ in range(H)]
    que = deque([])
    que.append((y, x))
    distance[y][x] = 0
    while que:
        y, x = que.popleft()
        if x == gx and y == gy:
            break
        for i, j in zip(dx, dy):
            nx = x + i
            ny = y + j
            if 0 <= nx < W and 0 <= ny < H and field[ny][nx] == "." and distance[ny][nx] == -1:
                distance[ny][nx] = distance[y][x] + 1
                que.append((ny, nx))
    return distance

ans = 0
for sy in range(H):
    for sx in range(W):
        if init_field[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if init_field[gy][gx] == "#":
                    continue
                field = [[i for i in row] for row in init_field]
                if sx == gx and sy == gy:
                    continue
                d = bfs(sx, sy)
                ans = max(ans, d[gy][gx])
print(ans)


Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
 
init_field = [list(input()) for _ in range(H)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
 
def bfs(x, y):
    distance = [[-1]*W for _ in range(H)]
    que = deque([])
    que.append((y, x))
    distance[y][x] = 0
    res = 0
    while que:
        y, x = que.popleft()
        res = max(res, distance[y][x])
        for i, j in zip(dx, dy):
            nx = x + i
            ny = y + j
            if 0 <= nx < W and 0 <= ny < H and field[ny][nx] == "." and distance[ny][nx] == -1:
                distance[ny][nx] = distance[y][x] + 1
                que.append((ny, nx))
    return res

ans = 0
for sy in range(H):
    for sx in range(W):
        if init_field[sy][sx] == '#':
            continue
        field = [[i for i in row] for row in init_field]
        d = bfs(sx, sy)
        ans = max(ans, d)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
H, W = map(int, input().split())
 
init_field = [list(input()) for _ in range(H)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
 
def bfs(x, y):
    distance = [[-1]*W for _ in range(H)]
    que = deque([])
    que.append((y, x))
    distance[y][x] = 0
    while que:
        y, x = que.popleft()
        if x == gx and y == gy:
            break
        for i, j in zip(dx, dy):
            nx = x + i
            ny = y + j
            if 0 <= nx < W and 0 <= ny < H and field[ny][nx] == "." and distance[ny][nx] == -1:
                distance[ny][nx] = distance[y][x] + 1
                que.append((ny, nx))
    return distance

ans = 0
for sy in range(H):
    for sx in range(W):
        if init_field[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if init_field[gy][gx] == "#":
                    continue
                field = [[i for i in row] for row in init_field]
                if sx == gx and sy == gy:
                    continue
                d = bfs(sx, sy)
                ans = max(ans, d[gy][gx])
print(ans)


Fixed submission:
from collections import deque
 
H, W = map(int, input().split())
 
init_field = [list(input()) for _ in range(H)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
 
def bfs(x, y):
    distance = [[-1]*W for _ in range(H)]
    que = deque([])
    que.append((y, x))
    distance[y][x] = 0
    res = 0
    while que:
        y, x = que.popleft()
        res = max(res, distance[y][x])
        for i, j in zip(dx, dy):
            nx = x + i
            ny = y + j
            if 0 <= nx < W and 0 <= ny < H and field[ny][nx] == "." and distance[ny][nx] == -1:
                distance[ny][nx] = distance[y][x] + 1
                que.append((ny, nx))
    return res

ans = 0
for sy in range(H):
    for sx in range(W):
        if init_field[sy][sx] == '#':
            continue
        field = [[i for i in row] for row in init_field]
        d = bfs(sx, sy)
        ans = max(ans, d)
print(ans)