from scipy.sparse.csgraph import dijkstra, csgraph_from_dense

from collections import defaultdict

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

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

dd = defaultdict(int)

for _ in range(h):

    for x in map(int, input().split()):

        if x == -1:

            continue

        dd[x] += 1



trans_table = list(zip(*table))

scores = dijkstra(csgraph_from_dense(trans_table), indices=1)



ans = 0

for k, v in list(dd.items()):

    ans += int(scores[k])*v

print(ans)
import heapq

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

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



trans_graph = list(zip(*graph))

next_nodes = []

for idx, d in enumerate(trans_graph[1]):

    next_nodes.append((d, idx))



heapq.heapify(next_nodes)



INF = 10**9

decided = [0]*10

decided[1] = 1

dist = [INF]*10

dist[1] = 0

while 0 in decided:

    distance, v = heapq.heappop(next_nodes)

    if decided[v]:

        continue

    decided[v] = 1

    dist[v] = distance

    for v2, x in enumerate(trans_graph[v]):

        if decided[v2]:

            continue

        heapq.heappush(next_nodes, (dist[v]+x, v2))



ans = 0

for _ in range(h):

    for x in map(int, input().split()):

        if x == -1:

            continue

        ans += dist[x]



print(ans)
from scipy.sparse.csgraph import dijkstra, csgraph_from_dense
from collections import defaultdict

h, w = list(map(int, input().split()))
table = [list(map(int, input().split())) for _ in range(10)]
dd = defaultdict(int)
for _ in range(h):
    for x in map(int, input().split()):
        if x == -1:
            continue
        dd[x] += 1
trans_table = list(zip(*table))
scores = dijkstra(csgraph_from_dense(trans_table), indices=1)
ans = 0
for k, v in list(dd.items()):
    ans += int(scores[k]) * v
print(ans)

import heapq

h, w = list(map(int, input().split()))
graph = [list(map(int, input().split())) for _ in range(10)]
trans_graph = list(zip(*graph))
next_nodes = []
for idx, d in enumerate(trans_graph[1]):
    next_nodes.append((d, idx))
heapq.heapify(next_nodes)
INF = 10**9
decided = [0] * 10
decided[1] = 1
dist = [INF] * 10
dist[1] = 0
while 0 in decided:
    distance, v = heapq.heappop(next_nodes)
    if decided[v]:
        continue
    decided[v] = 1
    dist[v] = distance
    for v2, x in enumerate(trans_graph[v]):
        if decided[v2]:
            continue
        heapq.heappush(next_nodes, (dist[v] + x, v2))
ans = 0
for _ in range(h):
    for x in map(int, input().split()):
        if x == -1:
            continue
        ans += dist[x]
print(ans)

-from scipy.sparse.csgraph import dijkstra, csgraph_from_dense -from collections import defaultdict +import heapq -table = [list(map(int, input().split())) for _ in range(10)] -dd = defaultdict(int) +graph = [list(map(int, input().split())) for _ in range(10)] +trans_graph = list(zip(*graph)) +next_nodes = [] +for idx, d in enumerate(trans_graph[1]): +    next_nodes.append((d, idx)) +heapq.heapify(next_nodes) +INF = 10**9 +decided = [0] * 10 +decided[1] = 1 +dist = [INF] * 10 +dist[1] = 0 +while 0 in decided: +    distance, v = heapq.heappop(next_nodes) +    if decided[v]: +        continue +    decided[v] = 1 +    dist[v] = distance +    for v2, x in enumerate(trans_graph[v]): +        if decided[v2]: +            continue +        heapq.heappush(next_nodes, (dist[v] + x, v2)) +ans = 0 -        dd[x] += 1 -trans_table = list(zip(*table)) -scores = dijkstra(csgraph_from_dense(trans_table), indices=1) -ans = 0 -for k, v in list(dd.items()): -    ans += int(scores[k]) * v +        ans += dist[x]