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

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

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



INF=10**10

tmp=[[INF]*10 for i in range(10)]

for k in range(10):

    for i in range(10):

        for j in range(10):

            tmp[i][j]=min(c[i][k]+c[k][j] for k in range(10))

            c[i][j]=min(tmp[i][j],c[i][j])



ans=0

for i in range(H):

    for j in range(W):

        if a[i][j]!=-1: ans+=c[a[i][j]][1]

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

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



INF=10**10

tmp=[[INF]*10 for i in range(10)]

for k in range(9):

    for i in range(10):

        for j in range(10):

            tmp[i][j]=min(c[i][k]+c[k][j] for k in range(10))

            c[i][j]=min(tmp[i][j],c[i][j])



count=[0]*10

for i in range(H):

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

    for j in range(10):

        count[j]+=l.count(j)

ans=0

for i in range(10):

    ans+=count[i]*c[i][1]



print(ans)
H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for i in range(10)]
a = [list(map(int, input().split())) for i in range(H)]
INF = 10**10
tmp = [[INF] * 10 for i in range(10)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            tmp[i][j] = min(c[i][k] + c[k][j] for k in range(10))
            c[i][j] = min(tmp[i][j], c[i][j])
ans = 0
for i in range(H):
    for j in range(W):
        if a[i][j] != -1:
            ans += c[a[i][j]][1]
print(ans)

H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for i in range(10)]
INF = 10**10
tmp = [[INF] * 10 for i in range(10)]
for k in range(9):
    for i in range(10):
        for j in range(10):
            tmp[i][j] = min(c[i][k] + c[k][j] for k in range(10))
            c[i][j] = min(tmp[i][j], c[i][j])
count = [0] * 10
for i in range(H):
    l = list(map(int, input().split()))
    for j in range(10):
        count[j] += l.count(j)
ans = 0
for i in range(10):
    ans += count[i] * c[i][1]
print(ans)

-a = [list(map(int, input().split())) for i in range(H)] -for k in range(10): +for k in range(9): +count = [0] * 10 +for i in range(H): +    l = list(map(int, input().split())) +    for j in range(10): +        count[j] += l.count(j) -for i in range(H): -    for j in range(W): -        if a[i][j] != -1: -            ans += c[a[i][j]][1] +for i in range(10): +    ans += count[i] * c[i][1]