from scipy.sparse.csgraph import johnson

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

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



ans = 0

for _ in range(h):

    for j in map(int, input().split()):

        ans += (j != -1) * int(c[j][1])

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



g = []

for _ in range(10):

    a = list(map(int, input().split()))

    g.append(a)



for k in range(10):

    for i in range(10):

        for j in range(10):

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



ans = 0

for i in range(h):

    ans += sum([g[x][1] for x in [x for x in map(int, input().split()) if x != -1]])

print(ans)

from scipy.sparse.csgraph import johnson

h, w = list(map(int, input().split()))
c = johnson([list(map(int, input().split())) for _ in range(10)])
ans = 0
for _ in range(h):
    for j in map(int, input().split()):
        ans += (j != -1) * int(c[j][1])
print(ans)

h, w = list(map(int, input().split()))
g = []
for _ in range(10):
    a = list(map(int, input().split()))
    g.append(a)
for k in range(10):
    for i in range(10):
        for j in range(10):
            g[i][j] = min(g[i][j], g[i][k] + g[k][j])
ans = 0
for i in range(h):
    ans += sum([g[x][1] for x in [x for x in map(int, input().split()) if x != -1]])
print(ans)

-from scipy.sparse.csgraph import johnson - -c = johnson([list(map(int, input().split())) for _ in range(10)]) +g = [] +for _ in range(10): +    a = list(map(int, input().split())) +    g.append(a) +for k in range(10): +    for i in range(10): +        for j in range(10): +            g[i][j] = min(g[i][j], g[i][k] + g[k][j]) -for _ in range(h): -    for j in map(int, input().split()): -        ans += (j != -1) * int(c[j][1]) +for i in range(h): +    ans += sum([g[x][1] for x in [x for x in map(int, input().split()) if x != -1]])