Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])
print(sum([sum(map(lambda a: 0 if a == -1 else C[a][1], row)) for row in A])

Fixed submission:
#http://abc079.contest.atcoder.jp/
# -*- coding: utf-8 -*-

h,w = map(int,input().split())

mp = []
for _ in range(10):
    mp.append(list(map(int,input().split())))

wall = []
for _ in range(h):
    wall.append(list(map(int,input().split())))

def min_cost(i,j,current,min_cost_current):
    if i == j: return current
    for index in range(10):
        if i == index: continue
        if current + mp[i][index] < min_cost_current:
            min_cost_current = min_cost(index,j,current+mp[i][index],min_cost_current)
    return min_cost_current


mp_min_cost =[]
for i in range(10):
    tmp = []
    for j in range(10):
        if i == j :
            tmp.append(0)
            continue

        min_cost_current = mp[i][j]
        if min_cost_current == sorted(mp[i])[1]:
            tmp.append(mp[i][j])
            continue

        # from i to j
        for index in range(10):
            if i == index: continue
            if mp[i][index] < min_cost_current:
                min_cost_current = min_cost(index,j,mp[i][index],min_cost_current)
        tmp.append(min_cost_current)
    mp_min_cost.append(tmp)

amount = 0
for i in wall:
    for j in i:
        if j == -1 or j == 1: continue
        amount += mp_min_cost[j][1]
print(amount)

Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]
for k in range(4):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])
print(sum([sum(map(lambda a: 0 if a == -1 else C[a][1], row)) for row in A])

Fixed submission:
#http://abc079.contest.atcoder.jp/
# -*- coding: utf-8 -*-

h,w = map(int,input().split())

mp = []
for _ in range(10):
    mp.append(list(map(int,input().split())))

wall = []
for _ in range(h):
    wall.append(list(map(int,input().split())))

def min_cost(i,j,current,min_cost_current):
    if i == j: return current
    for index in range(10):
        if i == index: continue
        if current + mp[i][index] < min_cost_current:
            min_cost_current = min_cost(index,j,current+mp[i][index],min_cost_current)
    return min_cost_current


mp_min_cost =[]
for i in range(10):
    tmp = []
    for j in range(10):
        if i == j :
            tmp.append(0)
            continue

        min_cost_current = mp[i][j]
        if min_cost_current == sorted(mp[i])[1]:
            tmp.append(mp[i][j])
            continue

        # from i to j
        for index in range(10):
            if i == index: continue
            if mp[i][index] < min_cost_current:
                min_cost_current = min_cost(index,j,mp[i][index],min_cost_current)
        tmp.append(min_cost_current)
    mp_min_cost.append(tmp)

amount = 0
for i in wall:
    for j in i:
        if j == -1 or j == 1: continue
        amount += mp_min_cost[j][1]
print(amount)