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

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

wall = [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 i in range(H):

    for j in range(W):

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

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



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

        a = A[h][w]

        if a != -1:

            ans += C[a][1]

print(ans)

H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
wall = [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 i in range(H):
    for j in range(W):
        if wall[i][j] != -1:
            ans += C[wall[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)]
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):
        a = A[h][w]
        if a != -1:
            ans += C[a][1]
print(ans)

-wall = [list(map(int, input().split())) for _ in range(H)] +A = [list(map(int, input().split())) for _ in range(H)] -for i in range(H): -    for j in range(W): -        if wall[i][j] != -1: -            ans += C[wall[i][j]][1] +for h in range(H): +    for w in range(W): +        a = A[h][w] +        if a != -1: +            ans += C[a][1]