from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import dijkstra

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(csr_matrix(trans_table), indices=1)

ans = 0

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

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

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)
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra
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(csr_matrix(trans_table), indices=1)
ans = 0
for k, v in list(dd.items()):
    ans += int(scores[k]) * v
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)

-from scipy.sparse import csr_matrix -from scipy.sparse.csgraph import dijkstra +from scipy.sparse.csgraph import dijkstra, csgraph_from_dense -scores = dijkstra(csr_matrix(trans_table), indices=1) +scores = dijkstra(csgraph_from_dense(trans_table), indices=1)