Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    s = [[-1] * W for _ in range(H)]
    d = [[0] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    s[sy][sx] = 1

    res = 0
    while que:
        x, y = que.popleft()
        s[y][x] = 1

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and s[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                d[ny][nx] = d[y][x] + 1
                res = max(res, d[ny][nx])

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)


Fixed submission:
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    d = [[-1] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    d[sy][sx] = 0

    res = 0
    while que:
        x, y = que.popleft()

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                tmp = d[y][x] + 1
                d[ny][nx] = tmp
                res = max(res, tmp)

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    s = [[-1] * W for _ in range(H)]
    d = [[0] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    s[sy][sx] = 1

    res = 0
    while que:
        x, y = que.popleft()
        s[y][x] = 1

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and s[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                d[ny][nx] = d[y][x] + 1
                res = max(res, d[ny][nx])

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            ans = max(ans, bfs(j, i))

print(ans)


Fixed submission:
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    d = [[-1] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    d[sy][sx] = 0

    res = 0
    while que:
        x, y = que.popleft()

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                tmp = d[y][x] + 1
                d[ny][nx] = tmp
                res = max(res, tmp)

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    s = [[-1] * W for _ in range(H)]
    d = [[0] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    s[sy][sx] = 1

    while que:
        x, y = que.popleft()
        s[y][x] = 1

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and s[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                d[ny][nx] = d[y][x] + 1

    return max([max(v) for v in d])

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            ans = max(ans, bfs(j, i))

print(ans)


Fixed submission:
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    d = [[-1] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    d[sy][sx] = 0

    res = 0
    while que:
        x, y = que.popleft()

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                tmp = d[y][x] + 1
                d[ny][nx] = tmp
                res = max(res, tmp)

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)

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

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            sx, sy = (j, i)
            s = [[-1] * W for _ in range(H)]
            d = [[0] * W for _ in range(H)]

            que = deque([])
            que.append((sx, sy))
            s[sy][sx] = 1
            
            while que:
                x, y = que.popleft()
                s[y][x] = 1

                for k in range(4):
                    nx = x + dx[k]
                    ny = y + dy[k]

                    if 0 <= nx < W and 0 <= ny < H and s[ny][nx] != 1 and c[ny][nx] == '.':
                        que.append((nx, ny))
                        d[ny][nx] = d[y][x] + 1
        ans = max(ans, max([max(v) for v in d]))

print(ans)


Fixed submission:
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    d = [[-1] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    d[sy][sx] = 0

    res = 0
    while que:
        x, y = que.popleft()

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                tmp = d[y][x] + 1
                d[ny][nx] = tmp
                res = max(res, tmp)

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
from itertools import combinations

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

points = []
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            points.append((j, i))

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]        
def bfs(start, goal):
    s = [[-1] * W for _ in range(H)]
    d = [[0] * W for _ in range(H)]

    sx, sy = start
    gx, gy = goal

    que = deque([])
    que.append((sx, sy))
    s[sy][sx] = 1
    
    while que:
        x, y = que.popleft()
        s[y][x] = 1

        if x == gx and y == gy:
            return d[y][x]
        
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < W and 0 <= ny < H and s[ny][nx] != 1 and c[ny][nx] == '.':
                que.append((nx, ny))
                d[ny][nx] = d[y][x] + 1

ans = 0
for comb in combinations(points, 2):
    start, goal = comb
    ans = max(ans, bfs(start, goal))

print(ans)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    d = [[-1] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    d[sy][sx] = 0

    res = 0
    while que:
        x, y = que.popleft()

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                tmp = d[y][x] + 1
                d[ny][nx] = tmp
                res = max(res, tmp)

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
from itertools import combinations

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

points = []
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            points.append((j, i))

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]        
def bfs(start, goal):
    s = [[-1] * W for _ in range(H)]
    d = [[0] * W for _ in range(H)]

    sx, sy = start
    gx, gy = goal

    que = deque([])
    que.append((sx, sy))
    s[sy][sx] = 1
    
    while que:
        x, y = que.popleft()
        s[y][x] = 1

        if x == gx and y == gy:
            return d[y][x]
        
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < W and 0 <= ny < H and s[ny][nx] != 1 and c[ny][nx] == '.':
                que.append((nx, ny))
                d[ny][nx] = d[y][x] + 1

ans = 0
for comb in combinations(points, 2):
    start, goal = comb
    ans = max(ans, bfs(start, goal))

print(ans)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
c = [list(input()) for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def bfs(sx, sy):
    d = [[-1] * W for _ in range(H)]

    que = deque([])
    que.append((sx, sy))
    d[sy][sx] = 0

    res = 0
    while que:
        x, y = que.popleft()

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == -1 and c[ny][nx] == '.':
                que.append((nx, ny))
                tmp = d[y][x] + 1
                d[ny][nx] = tmp
                res = max(res, tmp)

    return res

ans = 0
for i in range(H):
    for j in range(W):
        if c[i][j] == '.':
            tmp = bfs(j, i)
            ans = max(ans, tmp)

print(ans)