#!/usr/bin/env python3



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



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 i in range(H):

    for j in range(W):

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

            ans += c[A[i][j]][1]



print(ans)

#!/usr/bin/env python3

import itertools



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



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 = sum([c[a][1] for a in itertools.chain.from_iterable(A) if a >= 0])

print(ans)

#!/usr/bin/env python3
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)]
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 i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += c[A[i][j]][1]
print(ans)

#!/usr/bin/env python3
import itertools

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)]
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 = sum([c[a][1] for a in itertools.chain.from_iterable(A) if a >= 0])
print(ans)

+import itertools + -ans = 0 -for i in range(H): -    for j in range(W): -        if A[i][j] != -1: -            ans += c[A[i][j]][1] +ans = sum([c[a][1] for a in itertools.chain.from_iterable(A) if a >= 0])