from scipy.sparse.csgraph import floyd_warshall

from itertools import permutations



H, W = list(map(int, input().split()))

C = [[0]*10 for _ in range(10)]

A = [[0]*W for _ in range(H)]



for i in range(10):

  C[i] = list(map(int, input().split()))



C = floyd_warshall(C)  



cost = 0

for i in range(H):

  A[i] = list(map(int, input().split()))

  for a in A[i]:

    if a != -1:

      cost += C[a][1]



print((int(cost)))
# ABC79 D - Wall

from scipy.sparse.csgraph import floyd_warshall

from itertools import permutations



H, W = list(map(int, input().split()))

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

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



C = floyd_warshall(C)  



cost = 0

for i in range(H):

    for a in A[i]:

        if a != -1:

            cost += C[a][1]



print((int(cost)))
from scipy.sparse.csgraph import floyd_warshall
from itertools import permutations

H, W = list(map(int, input().split()))
C = [[0] * 10 for _ in range(10)]
A = [[0] * W for _ in range(H)]
for i in range(10):
    C[i] = list(map(int, input().split()))
C = floyd_warshall(C)
cost = 0
for i in range(H):
    A[i] = list(map(int, input().split()))
    for a in A[i]:
        if a != -1:
            cost += C[a][1]
print((int(cost)))

# ABC79 D - Wall
from scipy.sparse.csgraph import floyd_warshall
from itertools import permutations

H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]
C = floyd_warshall(C)
cost = 0
for i in range(H):
    for a in A[i]:
        if a != -1:
            cost += C[a][1]
print((int(cost)))

+# ABC79 D - Wall -C = [[0] * 10 for _ in range(10)] -A = [[0] * W for _ in range(H)] -for i in range(10): -    C[i] = list(map(int, input().split())) +C = [list(map(int, input().split())) for _ in range(10)] +A = [list(map(int, input().split())) for _ in range(H)] -    A[i] = list(map(int, input().split()))