Problem p02803 (python)

Fixed submission:
INF = 1001001001
from collections import deque
h,w = map(int,input().split())
s = [input() for _ in range(h)]
di = (-1, 0, 1, 0)
dj = ( 0,-1, 0, 1)

ans = 0
for si in range(h):
    for sj in range(w):
        if s[si][sj] == '#': continue
        dist = [[INF] * w for _ in range(h)]
        dist[si][sj] = 0
        q = deque([(si,sj)])
        while len(q) > 0:
            i,j = q.popleft()
            for dir in range(4):
                ni = i + di[dir]
                nj = j + dj[dir]
                if (ni<0 or ni>=h or nj<0 or nj>=w): continue
                if s[ni][nj] == '#': continue
                if dist[ni][nj] != INF: continue
                q.append((ni,nj))
                dist[ni][nj] = dist[i][j] + 1
        for i in range(h):
            for j in range(w):
                if dist[i][j] == INF: continue
                ans = max(ans, dist[i][j])
print(ans)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
# print(S)
INF = 10**9

V = H*W
d = [[INF for _ in range(V)] for _ in range(V)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            v = i*H + j
            d[v][v] = 0
            if i != 0 and S[i-1][j] == '.':
                x = (i-1)*H + j
                d[v][x] = 1
                d[x][v] = 1
            if i != H-1 and S[i+1][j] == '.':
                x = (i+1)*H + j
                d[v][x] = 1
                d[x][v] = 1
            if j != 0 and S[i][j-1] == '.':
                x = i*H + j-1
                d[v][x] = 1
                d[x][v] = 1
            if j != W-1 and S[i][j+1] == '.':
                x = i*H + j+1
                d[v][x] = 1
                d[x][v] = 1

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(V):
        for i in range(V):
            for j in range(V):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

d = warshall_floyd(d)
# print(d)
ans = 0
for i in range(V):
    for j in range(V):
        if d[i][j] != INF:
            ans = max(ans, d[i][j])

print(ans)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
# print(S)

V = H*W
d = [[float("inf") for _ in range(V)] for _ in range(V)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            v = i*H + j
            d[v][v] = 0
            if i != 0 and S[i-1][j] == '.':
                x = (i-1)*H + j
                d[v][x] = 1
                d[x][v] = 1
            if i != H-1 and S[i+1][j] == '.':
                x = (i+1)*H + j
                d[v][x] = 1
                d[x][v] = 1
            if j != 0 and S[i][j-1] == '.':
                x = i*H + j-1
                d[v][x] = 1
                d[x][v] = 1
            if j != W-1 and S[i][j+1] == '.':
                x = i*H + j+1
                d[v][x] = 1
                d[x][v] = 1

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(V):
        for i in range(V):
            for j in range(V):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

d = warshall_floyd(d)
# print(d)
ans = 0
for i in range(V):
    for j in range(V):
        if d[i][j] != float("inf"):
            ans = max(ans, d[i][j])

print(ans)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
# print(S)

V = H*W
d = [[float("inf") for _ in range(V)] for _ in range(V)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            v = i*H + j
            d[v][v] = 0
            if i != 0 and S[i-1][j] == '.':
                x = (i-1)*H + j
                d[v][x] = 1
                d[x][v] = 1
            if i != H-1 and S[i+1][j] == '.':
                x = (i+1)*H + j
                d[v][x] = 1
                d[x][v] = 1
            if j != 0 and S[i][j-1] == '.':
                x = i*H + j-1
                d[v][x] = 1
                d[x][v] = 1
            if j != W-1 and S[i][j+1] == '.':
                x = i*H + j+1
                d[v][x] = 1
                d[x][v] = 1

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(V):
        for i in range(V):
            for j in range(V):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

d = warshall_floyd(d)
# print(d)
ans = 0
for i in range(V):
    for j in range(V):
        if d[i][j] != float("inf"):
            ans = max(ans, d[i][j])

print(ans)