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



mincost=[0 for i in range(10)]



for i in range(10):

    if i!=1:

        res = {i:0}

        path = [(j,c[i][j]) for j in range(10) if j not in list(res.keys())]

        while 1 not in list(res.keys()):

            path.sort(key=lambda x:x[1])

            node,cost=path[0]

            res[node]=cost

            path = [p for p in path if p[0] not in list(res.keys())]

            path += [(j,cost+c[node][j]) for j in range(10) if j not in list(res.keys())]

        mincost[i]=res[1]



ans=0

from itertools import product

for i,j in product(list(range(H)),list(range(W))):

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

        ans+=mincost[A[i][j]]

print(ans)

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



from itertools import product



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

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

        c[i][j] = c[i][k]+c[k][j]

ans=0



for i,j in product(list(range(H)),list(range(W))):

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

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

print(ans)

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)]
mincost = [0 for i in range(10)]
for i in range(10):
    if i != 1:
        res = {i: 0}
        path = [(j, c[i][j]) for j in range(10) if j not in list(res.keys())]
        while 1 not in list(res.keys()):
            path.sort(key=lambda x: x[1])
            node, cost = path[0]
            res[node] = cost
            path = [p for p in path if p[0] not in list(res.keys())]
            path += [
                (j, cost + c[node][j]) for j in range(10) if j not in list(res.keys())
            ]
        mincost[i] = res[1]
ans = 0
from itertools import product

for i, j in product(list(range(H)), list(range(W))):
    if A[i][j] != -1:
        ans += mincost[A[i][j]]
print(ans)

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)]
from itertools import product

for k, i, j in product(list(range(10)), repeat=3):
    if c[i][j] > c[i][k] + c[k][j]:
        c[i][j] = c[i][k] + c[k][j]
ans = 0
for i, j in product(list(range(H)), list(range(W))):
    if A[i][j] != -1:
        ans += c[A[i][j]][1]
print(ans)

-mincost = [0 for i in range(10)] -for i in range(10): -    if i != 1: -        res = {i: 0} -        path = [(j, c[i][j]) for j in range(10) if j not in list(res.keys())] -        while 1 not in list(res.keys()): -            path.sort(key=lambda x: x[1]) -            node, cost = path[0] -            res[node] = cost -            path = [p for p in path if p[0] not in list(res.keys())] -            path += [ -                (j, cost + c[node][j]) for j in range(10) if j not in list(res.keys()) -            ] -        mincost[i] = res[1] -ans = 0 +for k, i, j in product(list(range(10)), repeat=3): +    if c[i][j] > c[i][k] + c[k][j]: +        c[i][j] = c[i][k] + c[k][j] +ans = 0 -        ans += mincost[A[i][j]] +        ans += c[A[i][j]][1]