import sys



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

C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]

A = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(H)]



for k in range(10):

    for i in range(10):

        for j in range(10):

            if C[i][j] > C[i][k] + C[k][j]:

                C[i][j] = C[i][k] + C[k][j]



res = 0

for i in range(H):

    for j in range(W):

        if A[i][j] >= 0:

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

print(res)

import sys



def warshall_floyd():

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



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

inf = float("inf")

C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]

A = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(H)]

warshall_floyd()

res = 0

for row in A:

    for col in row:

        if col != -1:

            res += C[col][1]



print(res)

import sys

H, W = list(map(int, input().split()))
C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]
A = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(H)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            if C[i][j] > C[i][k] + C[k][j]:
                C[i][j] = C[i][k] + C[k][j]
res = 0
for i in range(H):
    for j in range(W):
        if A[i][j] >= 0:
            res += C[A[i][j]][1]
print(res)

import sys


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


H, W = list(map(int, input().split()))
inf = float("inf")
C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]
A = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(H)]
warshall_floyd()
res = 0
for row in A:
    for col in row:
        if col != -1:
            res += C[col][1]
print(res)

+ +def warshall_floyd(): +    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]) + + +inf = float("inf") -for k in range(10): -    for i in range(10): -        for j in range(10): -            if C[i][j] > C[i][k] + C[k][j]: -                C[i][j] = C[i][k] + C[k][j] +warshall_floyd() -for i in range(H): -    for j in range(W): -        if A[i][j] >= 0: -            res += C[A[i][j]][1] +for row in A: +    for col in row: +        if col != -1: +            res += C[col][1]