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

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

import queue



def dist(i):

    res = c[i][1]

    que = queue.Queue()

    path = []

    for j in range(10):

        if j != i and j != 1:

            path.append(j)

    for j in range(10):

        if j != 1 and j != i and c[i][j] < res:

            jpath = path.copy()

            jpath.pop(jpath.index(j))

            que.put((j,c[i][j],jpath))

    while que.qsize():

        j,cost,path = que.get()

        if cost + c[j][1] < res:

            res = cost + c[j][1]

        for k in path:

            if k != 1 and cost+c[j][k] < res:

                kpath = path.copy()

                kpath.pop(kpath.index(k))

                que.put((k,cost+c[j][k],kpath))

    return res



cost = []

for i in range(10):

    cost.append(dist(i))



ans = 0

for i in range(H):

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

    for j in range(W):

        if A[j] != -1:

            ans += cost[A[j]]

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

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

import queue







d = c.copy()

for k in range(10):

    for i in range(10):

        for j in range(10):

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

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



cost = []

for i in range(10):

    cost.append(d[i][1])



ans = 0

for i in range(H):

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

    for j in range(W):

        if A[j] != -1:

            ans += cost[A[j]]

print(ans)
H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for i in range(10)]
import queue


def dist(i):
    res = c[i][1]
    que = queue.Queue()
    path = []
    for j in range(10):
        if j != i and j != 1:
            path.append(j)
    for j in range(10):
        if j != 1 and j != i and c[i][j] < res:
            jpath = path.copy()
            jpath.pop(jpath.index(j))
            que.put((j, c[i][j], jpath))
    while que.qsize():
        j, cost, path = que.get()
        if cost + c[j][1] < res:
            res = cost + c[j][1]
        for k in path:
            if k != 1 and cost + c[j][k] < res:
                kpath = path.copy()
                kpath.pop(kpath.index(k))
                que.put((k, cost + c[j][k], kpath))
    return res


cost = []
for i in range(10):
    cost.append(dist(i))
ans = 0
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        if A[j] != -1:
            ans += cost[A[j]]
print(ans)

H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for i in range(10)]
import queue

d = c.copy()
for k in range(10):
    for i in range(10):
        for j in range(10):
            if d[i][j] > d[i][k] + d[k][j]:
                d[i][j] = d[i][k] + d[k][j]
cost = []
for i in range(10):
    cost.append(d[i][1])
ans = 0
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        if A[j] != -1:
            ans += cost[A[j]]
print(ans)

- -def dist(i): -    res = c[i][1] -    que = queue.Queue() -    path = [] -    for j in range(10): -        if j != i and j != 1: -            path.append(j) -    for j in range(10): -        if j != 1 and j != i and c[i][j] < res: -            jpath = path.copy() -            jpath.pop(jpath.index(j)) -            que.put((j, c[i][j], jpath)) -    while que.qsize(): -        j, cost, path = que.get() -        if cost + c[j][1] < res: -            res = cost + c[j][1] -        for k in path: -            if k != 1 and cost + c[j][k] < res: -                kpath = path.copy() -                kpath.pop(kpath.index(k)) -                que.put((k, cost + c[j][k], kpath)) -    return res - - +d = c.copy() +for k in range(10): +    for i in range(10): +        for j in range(10): +            if d[i][j] > d[i][k] + d[k][j]: +                d[i][j] = d[i][k] + d[k][j] -    cost.append(dist(i)) +    cost.append(d[i][1])