Problem p02803 (python)

Fixed submission:
import sys
from pprint import pprint

def warshall_floyd(d, n):
    # pprint(d)
    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

def solve(h, w, s):
    
    n_node = 0
    nodes = [] 
    # pprint(nodes)
    for y in range(h):
        nodes.append([])
        for x in range(w):
            nodes[y].append(n_node)
            n_node += 1
    edges = [[float("inf") for i in range(n_node)] for j in range(n_node)]
    # pprint(nodes)
    for y in range(h):
        for x in range(w):
            if s[y][x] != ".":
                continue
            for dx, dy in ((1,0), (-1,0), (0,1), (0,-1)):
                if 0 <= y+dy < h and 0 <= x+dx < w and s[y+dy][x+dx] == ".":
                    edges[nodes[y][x]][nodes[y+dy][x+dx]] = 1
    for i in range(n_node):
        edges[i][i] = 0
    
    dist = warshall_floyd(edges, n_node)
    # pprint(dist)
    ans = -1
    for y in range(n_node):
        for x in range(n_node):
            if dist[y][x] < float("inf"):
                ans = max(ans, dist[y][x])
    print(ans)


if __name__ == '__main__':
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    s = []
    for i in range(h):
        s.append(list(sys.stdin.readline().strip()))
    # debug
    # pprint(s)
    solve(h, w, s)

Buggy submission (verdict: Runtime Error):
import sys
from pprint import pprint

def warshall_floyd(d, n):
    # pprint(d)
    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

def solve(h, w, s):
    
    edges = {}
    n_node = 0

    nodes = [] 
    # pprint(nodes)
    for y in range(h):
        nodes.append([])
        for x in range(w):
            nodes[y].append(n_node)
            n_node += 1
    edges = [[float("inf") for i in range(n_node)] for j in range(n_node)]
    # pprint(nodes)
    for y in range(h):
        for x in range(w):
            for dx, dy in ((1,0), (-1,0), (0,1), (0,-1)):
                if s[y][x] != ".":
                    continue
                if 0 <= y+dy < h and 0 <= x+dx < w and s[y+dy][x+dx] == ".":
                    edges[nodes[y][x]][nodes[y+dy][x+dx]] = 1
    for i in range(n_node):
        edges[i][i] = 0
    
    dist = warshall_floyd(edges, n_node)
    # pprint(dist)
    ans = -1
    for y in range(n_node):
        for x in range(n_node):
            if dist[y][x] < float("inf"):
                ans = max(ans, dist[y][x])
    print(ans)


if __name__ == '__main__':
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    s = []
    for i in range(w):
        s.append(list(sys.stdin.readline().strip()))
    # debug
    # pprint(s)
    solve(h, w, s)

Buggy submission (verdict: Runtime Error):
import sys
from pprint import pprint

def warshall_floyd(d, n):
    # pprint(d)
    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

def solve(h, w, s):
    
    edges = {}
    n_node = 0

    nodes = [] 
    # pprint(nodes)
    for y in range(h):
        nodes.append([])
        for x in range(w):
            nodes[y].append(n_node)
            n_node += 1
    edges = [[float("inf") for i in range(n_node)] for j in range(n_node)]
    # pprint(nodes)
    for y in range(h):
        for x in range(w):
            for dx, dy in ((1,0), (-1,0), (0,1), (0,-1)):
                if s[y][x] != ".":
                    continue
                if 0 <= y+dy < h and 0 <= x+dx < w and s[y+dy][x+dx] == ".":
                    edges[nodes[y][x]][nodes[y+dy][x+dx]] = 1
    
    dist = warshall_floyd(edges, n_node)
    # pprint(dist)
    ans = -1
    for y in range(n_node):
        for x in range(n_node):
            if dist[y][x] < float("inf"):
                ans = max(ans, dist[y][x])
    print(ans)


if __name__ == '__main__':
    h, w = map(int, sys.stdin.readline().strip().split(" "))
    s = []
    for i in range(w):
        s.append(list(sys.stdin.readline().strip()))
    # debug
    # pprint(s)
    solve(h, w, s)