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

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



A = []

for _ in range(H):

    A += list(map(int, input().split()))



for i in range(10):

    for j in range(10):

        for k in range(10):

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



from collections import Counter

Co = Counter(A)

ans = 0

for a, c in Co.most_common():

    if a==-1 or a==1:

        continue

    ans += C[a][1]*c

print(ans)
import sys

def input(): return sys.stdin.readline().strip()

def mapint(): return list(map(int, input().split()))

sys.setrecursionlimit(10**9)



from heapq import heappop, heappush

H, W = mapint()

Cs = [list(mapint()) for _ in range(10)]

lis = []

for h in range(H):

    lis.extend(list(mapint()))



def dijkstra(start):

    Q = [(0, start)]

    checked = [0]*10

    while Q:

        d, v = heappop(Q)

        checked[v] = 1

        if v==1:

            return d

        for i in range(10):

            if not checked[i]:

                heappush(Q, (d+Cs[v][i], i))



costs = [0]*10

for i in range(10):

    costs[i] = dijkstra(i)



from collections import Counter

c = Counter(lis)

ans = 0

for i in range(10):

    ans += costs[i]*c[i]

print(ans)

        
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
A = []
for _ in range(H):
    A += list(map(int, input().split()))
for i in range(10):
    for j in range(10):
        for k in range(10):
            C[j][k] = min(C[j][k], C[j][i] + C[i][k])
from collections import Counter

Co = Counter(A)
ans = 0
for a, c in Co.most_common():
    if a == -1 or a == 1:
        continue
    ans += C[a][1] * c
print(ans)

import sys


def input():
    return sys.stdin.readline().strip()


def mapint():
    return list(map(int, input().split()))


sys.setrecursionlimit(10**9)
from heapq import heappop, heappush

H, W = mapint()
Cs = [list(mapint()) for _ in range(10)]
lis = []
for h in range(H):
    lis.extend(list(mapint()))


def dijkstra(start):
    Q = [(0, start)]
    checked = [0] * 10
    while Q:
        d, v = heappop(Q)
        checked[v] = 1
        if v == 1:
            return d
        for i in range(10):
            if not checked[i]:
                heappush(Q, (d + Cs[v][i], i))


costs = [0] * 10
for i in range(10):
    costs[i] = dijkstra(i)
from collections import Counter

c = Counter(lis)
ans = 0
for i in range(10):
    ans += costs[i] * c[i]
print(ans)

-H, W = list(map(int, input().split())) -C = [list(map(int, input().split())) for _ in range(10)] -A = [] -for _ in range(H): -    A += list(map(int, input().split())) +import sys + + +def input(): +    return sys.stdin.readline().strip() + + +def mapint(): +    return list(map(int, input().split())) + + +sys.setrecursionlimit(10**9) +from heapq import heappop, heappush + +H, W = mapint() +Cs = [list(mapint()) for _ in range(10)] +lis = [] +for h in range(H): +    lis.extend(list(mapint())) + + +def dijkstra(start): +    Q = [(0, start)] +    checked = [0] * 10 +    while Q: +        d, v = heappop(Q) +        checked[v] = 1 +        if v == 1: +            return d +        for i in range(10): +            if not checked[i]: +                heappush(Q, (d + Cs[v][i], i)) + + +costs = [0] * 10 -    for j in range(10): -        for k in range(10): -            C[j][k] = min(C[j][k], C[j][i] + C[i][k]) +    costs[i] = dijkstra(i) -Co = Counter(A) +c = Counter(lis) -for a, c in Co.most_common(): -    if a == -1 or a == 1: -        continue -    ans += C[a][1] * c +for i in range(10): +    ans += costs[i] * c[i]