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

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

for t in range(10):

	for i in range(10):

		for j in range(10):

			for k in range(10):

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

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



print(sum(sum(d[a][1] if a>=0 else 0 for a in aa) for aa in arr))
h,w = list(map(int,input().split()))

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



for k in range(10):

	for i in range(10):

		for j in range(10):

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



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



print(sum(sum(d[a][1] if a>=0 else 0 for a in aa) for aa in arr))
h, w = list(map(int, input().split()))
d = [list(map(int, input().split())) for i in range(10)]
for t in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
arr = [list(map(int, input().split())) for i in range(h)]
print(sum(sum(d[a][1] if a >= 0 else 0 for a in aa) for aa in arr))

h, w = list(map(int, input().split()))
d = [list(map(int, input().split())) for i in range(10)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
arr = [list(map(int, input().split())) for i in range(h)]
print(sum(sum(d[a][1] if a >= 0 else 0 for a in aa) for aa in arr))

-for t in range(10): +for k in range(10): -            for k in range(10): -                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) +            d[i][j] = min(d[i][j], d[i][k] + d[k][j])