INFTY = 10**5

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

dist = [[INFTY for _ in range(10)] for _ in range(10)]

for i in range(10):

    dist[i][i] = 0

for i in range(10):

    for j in range(10):

        dist[i][j] = C[i][j]

for k in range(10):

    for i in range(10):

        for j in range(10):

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

cnt = 0

for i in range(H):

    for j in range(W):

        a = A[i][j]

        if a!=-1:

            cnt += dist[a][1]

print(cnt)
INFTY = 10**4+10

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

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

cost = [[INFTY for _ in range(10)] for _ in range(10)]

for i in range(10):

    for j in range(10):

        cost[i][j] = C[i][j]

for k in range(10):

    for i in range(10):

        for j in range(10):

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

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

tot = 0

for i in range(H):

    for j in range(W):

        if A[i][j]==1 or A[i][j]==-1:continue

        tot += cost[A[i][j]][1]

print(tot)
INFTY = 10**5
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)]
dist = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    dist[i][i] = 0
for i in range(10):
    for j in range(10):
        dist[i][j] = C[i][j]
for k in range(10):
    for i in range(10):
        for j in range(10):
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
cnt = 0
for i in range(H):
    for j in range(W):
        a = A[i][j]
        if a != -1:
            cnt += dist[a][1]
print(cnt)

INFTY = 10**4 + 10
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
cost = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        cost[i][j] = C[i][j]
for k in range(10):
    for i in range(10):
        for j in range(10):
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
A = [list(map(int, input().split())) for _ in range(H)]
tot = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == 1 or A[i][j] == -1:
            continue
        tot += cost[A[i][j]][1]
print(tot)

-INFTY = 10**5 +INFTY = 10**4 + 10 -A = [list(map(int, input().split())) for _ in range(H)] -dist = [[INFTY for _ in range(10)] for _ in range(10)] -for i in range(10): -    dist[i][i] = 0 +cost = [[INFTY for _ in range(10)] for _ in range(10)] -        dist[i][j] = C[i][j] +        cost[i][j] = C[i][j] -            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) -cnt = 0 +            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]) +A = [list(map(int, input().split())) for _ in range(H)] +tot = 0 -        a = A[i][j] -        if a != -1: -            cnt += dist[a][1] -print(cnt) +        if A[i][j] == 1 or A[i][j] == -1: +            continue +        tot += cost[A[i][j]][1] +print(tot)