Buggy python submission (verdict: Time Limit Exceeded):
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)]

mp2 = []
for i in range(10):
    for j in range(10):
        mp2.append([i, j, c[i][j]])

n_v = 10
inf = float('inf')

# コストテーブルの作成
from heapq import heappop, heappush
cost = dict() # コストテーブル
for i, j, k in mp2:
    cost[i] = cost.get(i, []) + [(j, k)]

total = 0
for i in range(len(a)):
    _a = a[i]
    for j in range(len(_a)):
        if _a[j] != -1:
            start = _a[j]

            d = [inf for i in range(n_v)] # 最短距離
            d[start] = 0

            q = [] # min-heap, (距離, 頂点)
            heappush(q, (d[start], start))
            while q:
                du, u = heappop(q) # 最短距離とその頂点
                if d[u] < du:
                    continue
                for v, weight in cost.get(u, []):
                    alt = du + weight
                    if d[v] > alt:
                        d[v] = alt
                        heappush(q, (alt, v))

            total += d[1]

print(total)


Fixed submission:
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)]

from scipy.sparse.csgraph import floyd_warshall
cost=floyd_warshall(c)

total = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1:
            start = a[i][j]
            d = cost[start][1]
            total += int(d)

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
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)]

mp2 = []
for i in range(10):
    for j in range(10):
        mp2.append([i, j, c[i][j]])
n_v = 10
from heapq import heappop, heappush

total = 0
for i in range(len(a)):
    _a = a[i]
    for j in range(len(_a)):
        if _a[j] != -1:
            start = _a[j]

            # コストテーブルの作成
            inf = float('inf')
            d = [inf for i in range(n_v)] # 最短距離
            d[start] = 0
            prev = [None] * n_v # 経路をたどるときに使う
            cost = dict() # コストテーブル
            for i, j, k in mp2:
                cost[i] = cost.get(i, []) + [(j, k)]

            q = [] # min-heap, (距離, 頂点)
            heappush(q, (d[start], start))
            while q:
                # print(q, d, prev)
                du, u = heappop(q) # 最短距離とその頂点
                if d[u] < du:
                    continue
                for v, weight in cost.get(u, []):
                    alt = du + weight
                    if d[v] > alt:
                        d[v] = alt
                        prev[v] = u
                        heappush(q, (alt, v))

            total += d[1]

print(total)


Fixed submission:
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)]

from scipy.sparse.csgraph import floyd_warshall
cost=floyd_warshall(c)

total = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1:
            start = a[i][j]
            d = cost[start][1]
            total += int(d)

print(total)