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

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

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



for k in range(10):

    for i in range(10):

        for j in range(10):

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



ans = 0

for row in G:

    for x in row:

        if x >= 0:

            ans += C[x][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)]





for k in range(10):

    for i in range(10):

        for j in range(10):

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





ans = 0

for h in range(H):

    for w in range(W):

        if A[h][w] == -1:

            continue

        ans += C[A[h][w]][1]



print(ans)

H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for i in range(10)]
G = [list(map(int, input().split())) for i in range(H)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])
ans = 0
for row in G:
    for x in row:
        if x >= 0:
            ans += C[x][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)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])
ans = 0
for h in range(H):
    for w in range(W):
        if A[h][w] == -1:
            continue
        ans += C[A[h][w]][1]
print(ans)

-C = [list(map(int, input().split())) for i in range(10)] -G = [list(map(int, input().split())) for i in range(H)] +C = [list(map(int, input().split())) for _ in range(10)] +A = [list(map(int, input().split())) for _ in range(H)] -for row in G: -    for x in row: -        if x >= 0: -            ans += C[x][1] +for h in range(H): +    for w in range(W): +        if A[h][w] == -1: +            continue +        ans += C[A[h][w]][1]