h,w = list(map(int,input().split()))

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

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

ans = 0

for rep in range(10):

    for i in range(10):

        for j in range(10):

            for k in range(10):

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

                    l[i][j] = l[i][k]+l[k][j]

num = [i[1] for i in l] + [0]

for i in a:

    for j in i:

        ans += num[j]

print(ans)
h,w = list(map(int,input().split()))

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

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

ans = 0

for k in range(10):

    for i in range(10):

        for j in range(10):

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

                l[i][j] = l[i][k]+l[k][j]

num = [i[1] for i in l] + [0]

for i in a:

    for j in i:

        ans += num[j]

print(ans)
h, w = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(10)]
a = [list(map(int, input().split())) for i in range(h)]
ans = 0
for rep in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if l[i][j] > l[i][k] + l[k][j]:
                    l[i][j] = l[i][k] + l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)

h, w = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(10)]
a = [list(map(int, input().split())) for i in range(h)]
ans = 0
for k in range(10):
    for i in range(10):
        for j in range(10):
            if l[i][j] > l[i][k] + l[k][j]:
                l[i][j] = l[i][k] + l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)

-for rep in range(10): +for k in range(10): -            for k in range(10): -                if l[i][j] > l[i][k] + l[k][j]: -                    l[i][j] = l[i][k] + l[k][j] +            if l[i][j] > l[i][k] + l[k][j]: +                l[i][j] = l[i][k] + l[k][j]