Problem p02803 (python)

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


H, W = map(int, input().split())

fld = [""] * H
for i in range(H):
    fld[i] = input()

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

d = [[float("inf") for i in range(H * W)] for i in range(H * W)]
for i in range(H):
    for j in range(W):
        if fld[i][j] == '#':
            continue
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H or y < 0 or y >= W:
                continue
            if fld[x][y] == ".":
                d[i*W+j][x*W+y] = 1


for i in range(H*W):
    d[i][i] = 0

warshall = warshall_floyd(d, H*W)
ans = 0
for val in warshall:
    for v in val:
        if v != float("inf"):
            ans = max(ans, v)

print(ans)

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


H, W = map(int, input().split())

fld = [""] * H
for i in range(H):
    fld[i] = input()

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

d = [[float("inf") for i in range(H * W+1)] for i in range(H * W+1)]
for i in range(H):
    for j in range(W):
        if fld[i][j] == '#':
            continue
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H or y < 0 or y >= W:
                continue
            if fld[x][y] == ".":
                d[i*H+j][x*H+y] = 1


for i in range(H*W):
    d[i][i] = 0

warshall = warshall_floyd(d, H*W)
ans = 0
for val in warshall:
    for v in val:
        if v != float("inf"):
            ans = max(ans, v)

print(ans)

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


H, W = map(int, input().split())

fld = [""] * H
for i in range(H):
    fld[i] = input()

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

d = [[float("inf") for i in range(H * W)] for i in range(H * W)]
for i in range(H):
    for j in range(W):
        if fld[i][j] == '#':
            continue
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H or y < 0 or y >= W:
                continue
            if fld[x][y] == ".":
                d[i*H+j][x*H+y] = 1


for i in range(H*W):
    d[i][i] = 0

warshall = warshall_floyd(d, H*W)
ans = 0
for val in warshall:
    for v in val:
        if v != float("inf"):
            ans = max(ans, v)

print(ans)

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


H, W = map(int, input().split())

fld = [""] * H
for i in range(H):
    fld[i] = input()

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

d = [[float("inf") for i in range(H * W)] for i in range(H * W)]
for i in range(H):
    for j in range(W):
        if fld[i][j] == '#':
            continue
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H or y < 0 or y >= W:
                continue
            if fld[x][y] == ".":
                d[i*H+j][x*H+y] = 1


for i in range(H*W):
    d[i][i] = 0

warshall = warshall_floyd(d, H*W)
ans = 0
for val in warshall:
    for v in val:
        if v != float("inf"):
            ans = max(ans, v)

print(ans)