# https://atcoder.jp/contests/abc079/tasks/abc079_d



from collections import Counter

from functools import reduce



def warshall(d, n):

    for k in range(n):

        for i in range(n):

            for j in range(n):

                if d[i][k] + d[k][j] < d[i][j]:

                    d[i][j] = d[i][k] + d[k][j]



h,w = list(map(int,input().split()))

magic = []

for i in range(10):

    magic.append(list(map(int,input().split())))



counter = {i:0 for i in range(10)}

for _ in range(h):

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

        if a > -1:

            counter[a] += 1

    # counter.update(Counter(map(int, input().split())))

warshall(magic, 10)



ans = 0

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

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

print(ans)

# print(reduce(lambda x,y:x+y, [magic[k][1] * v for k,v in counter.items() if k > -1]))

# https://atcoder.jp/contests/abc079/tasks/abc079_d



from collections import Counter

from functools import reduce



def warshall(d, n):

    for k in range(n):

        for i in range(n):

            for j in range(n):

                if d[i][k] + d[k][j] < d[i][j]:

                    d[i][j] = d[i][k] + d[k][j]



h,w = list(map(int,input().split()))

magic = []

for i in range(10):

    magic.append(list(map(int,input().split())))



counter = Counter()

for _ in range(h):

    counter.update(Counter(list(map(int, input().split()))))

warshall(magic, 10)

print((reduce(lambda x,y:x+y, [magic[k][1] * v for k,v in list(counter.items()) if k > -1])))

# https://atcoder.jp/contests/abc079/tasks/abc079_d
from collections import Counter
from functools import reduce


def warshall(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]


h, w = list(map(int, input().split()))
magic = []
for i in range(10):
    magic.append(list(map(int, input().split())))
counter = {i: 0 for i in range(10)}
for _ in range(h):
    for a in map(int, input().split()):
        if a > -1:
            counter[a] += 1
    # counter.update(Counter(map(int, input().split())))
warshall(magic, 10)
ans = 0
for k, v in list(counter.items()):
    ans += magic[k][1] * v
print(ans)
# print(reduce(lambda x,y:x+y, [magic[k][1] * v for k,v in counter.items() if k > -1]))

# https://atcoder.jp/contests/abc079/tasks/abc079_d
from collections import Counter
from functools import reduce


def warshall(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]


h, w = list(map(int, input().split()))
magic = []
for i in range(10):
    magic.append(list(map(int, input().split())))
counter = Counter()
for _ in range(h):
    counter.update(Counter(list(map(int, input().split()))))
warshall(magic, 10)
print(
    (
        reduce(
            lambda x, y: x + y,
            [magic[k][1] * v for k, v in list(counter.items()) if k > -1],
        )
    )
)

-counter = {i: 0 for i in range(10)} +counter = Counter() -    for a in map(int, input().split()): -        if a > -1: -            counter[a] += 1 -    # counter.update(Counter(map(int, input().split()))) +    counter.update(Counter(list(map(int, input().split())))) -ans = 0 -for k, v in list(counter.items()): -    ans += magic[k][1] * v -print(ans) -# print(reduce(lambda x,y:x+y, [magic[k][1] * v for k,v in counter.items() if k > -1])) +print( +    ( +        reduce( +            lambda x, y: x + y, +            [magic[k][1] * v for k, v in list(counter.items()) if k > -1], +        ) +    ) +)