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

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

cntA = {}

for _ in range(H):

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

    if a not in cntA:

      cntA[a] = 1

    else:

      cntA[a] += 1



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

      

ans = 0

for key, num in list(cntA.items()):

  if key not in [-1,1]:

    ans += C[key][1]*num

print(ans)
import copy

from collections import Counter



def make_table():

    dp = copy.deepcopy(C)



    for k in range(N):

        for i in range(N):

            for j in range(N):

                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

    return dp





N = 10

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

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



A = []

for _ in range(H):

    A.extend(list(map(int, input().split())))



cntA = Counter(A)



dp = make_table()



print((sum([dp[key][1] * cntA[key] if key != -1 else 0 for key in cntA])))
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
cntA = {}
for _ in range(H):
    for a in map(int, input().split()):
        if a not in cntA:
            cntA[a] = 1
        else:
            cntA[a] += 1
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])
ans = 0
for key, num in list(cntA.items()):
    if key not in [-1, 1]:
        ans += C[key][1] * num
print(ans)

import copy
from collections import Counter


def make_table():
    dp = copy.deepcopy(C)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
    return dp


N = 10
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(N)]
A = []
for _ in range(H):
    A.extend(list(map(int, input().split())))
cntA = Counter(A)
dp = make_table()
print((sum([dp[key][1] * cntA[key] if key != -1 else 0 for key in cntA])))

+import copy +from collections import Counter + + +def make_table(): +    dp = copy.deepcopy(C) +    for k in range(N): +        for i in range(N): +            for j in range(N): +                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]) +    return dp + + +N = 10 -C = [list(map(int, input().split())) for _ in range(10)] -cntA = {} +C = [list(map(int, input().split())) for _ in range(N)] +A = [] -    for a in map(int, input().split()): -        if a not in cntA: -            cntA[a] = 1 -        else: -            cntA[a] += 1 -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]) -ans = 0 -for key, num in list(cntA.items()): -    if key not in [-1, 1]: -        ans += C[key][1] * num -print(ans) +    A.extend(list(map(int, input().split()))) +cntA = Counter(A) +dp = make_table() +print((sum([dp[key][1] * cntA[key] if key != -1 else 0 for key in cntA])))