from collections import defaultdict

from itertools import product



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

c = [[int(x) for x in input().split()] for _ in range(10)]

A = defaultdict(int)

for _ in range(H):

    for x in input().split():

        x = int(x)

        if x != -1 and x != 1:

            A[x] += 1



for _, i, j, k in product(list(range(10)), list(range(10)), list(range(10)), list(range(10))):

    if i == 1 or k == 1:

        continue

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



ans = 0

for k, v in list(A.items()):

    ans += c[k][1] * v

print(ans)
from collections import defaultdict

from itertools import product



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

c = [[int(x) for x in input().split()] for _ in range(10)]

A = defaultdict(int)

for _ in range(H):

    for x in input().split():

        x = int(x)

        if x != -1 and x != 1:

            A[x] += 1



for k, i, j in product(list(range(10)), list(range(10)), list(range(10))):

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



ans = 0

for k, v in list(A.items()):

    ans += c[k][1] * v

print(ans)
from collections import defaultdict
from itertools import product

H, W = list(map(int, input().split()))
c = [[int(x) for x in input().split()] for _ in range(10)]
A = defaultdict(int)
for _ in range(H):
    for x in input().split():
        x = int(x)
        if x != -1 and x != 1:
            A[x] += 1
for _, i, j, k in product(
    list(range(10)), list(range(10)), list(range(10)), list(range(10))
):
    if i == 1 or k == 1:
        continue
    c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for k, v in list(A.items()):
    ans += c[k][1] * v
print(ans)

from collections import defaultdict
from itertools import product

H, W = list(map(int, input().split()))
c = [[int(x) for x in input().split()] for _ in range(10)]
A = defaultdict(int)
for _ in range(H):
    for x in input().split():
        x = int(x)
        if x != -1 and x != 1:
            A[x] += 1
for k, i, j in product(list(range(10)), list(range(10)), list(range(10))):
    c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for k, v in list(A.items()):
    ans += c[k][1] * v
print(ans)

-for _, i, j, k in product( -    list(range(10)), list(range(10)), list(range(10)), list(range(10)) -): -    if i == 1 or k == 1: -        continue +for k, i, j in product(list(range(10)), list(range(10)), list(range(10))):