inf = 1000

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



dp = []

for i in range(10):

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

    dp.append(c)



for k in range(10):

    for i in range(10):

        for j in range(10):

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



ans = 0

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



for i in range(h):

    for j in range(w):

        if A[i][j] not in [-1,1]:

            ans += dp[A[i][j]][1]



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



C = []

for i in range(10):

    C.append(list(map(int,input().split())))



def warshall_floyd(d):

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

    return d



trans_map = warshall_floyd(C)



ans = 0

for i in range(h):

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

    for j in range(w):

        num = line[j]

        if num == -1:

            continue

        ans += trans_map[num][1]



print(ans)
inf = 1000
h, w = list(map(int, input().split()))
dp = []
for i in range(10):
    c = list(map(int, input().split()))
    dp.append(c)
for k in range(10):
    for i in range(10):
        for j in range(10):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
ans = 0
A = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if A[i][j] not in [-1, 1]:
            ans += dp[A[i][j]][1]
print(ans)

h, w = list(map(int, input().split()))
C = []
for i in range(10):
    C.append(list(map(int, input().split())))


def warshall_floyd(d):
    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])
    return d


trans_map = warshall_floyd(C)
ans = 0
for i in range(h):
    line = list(map(int, input().split()))
    for j in range(w):
        num = line[j]
        if num == -1:
            continue
        ans += trans_map[num][1]
print(ans)

-inf = 1000 -dp = [] +C = [] -    c = list(map(int, input().split())) -    dp.append(c) -for k in range(10): -    for i in range(10): -        for j in range(10): -            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]) +    C.append(list(map(int, input().split()))) + + +def warshall_floyd(d): +    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]) +    return d + + +trans_map = warshall_floyd(C) -A = [list(map(int, input().split())) for _ in range(h)] +    line = list(map(int, input().split())) -        if A[i][j] not in [-1, 1]: -            ans += dp[A[i][j]][1] +        num = line[j] +        if num == -1: +            continue +        ans += trans_map[num][1]