# ABC79 D - Wall

from scipy.sparse.csgraph import floyd_warshall



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

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)]



def f_warshall(C):

  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])



f_warshall(C)



cost = 0

for i in range(H):

  for j in range(W):

    if A[i][j] == -1:

      continue

    else:

      cost += C[A[i][j]][1]



print((int(cost)))
# ABC79 D - Wall
from scipy.sparse.csgraph import floyd_warshall

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
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)]


def f_warshall(C):
    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])


f_warshall(C)
cost = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == -1:
            continue
        else:
            cost += C[A[i][j]][1]
print((int(cost)))

-from scipy.sparse.csgraph import floyd_warshall - -C = floyd_warshall(C) + + +def f_warshall(C): +    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]) + + +f_warshall(C) -    for a in A[i]: -        if a != -1: -            cost += C[a][1] +    for j in range(W): +        if A[i][j] == -1: +            continue +        else: +            cost += C[A[i][j]][1]