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



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

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



"""

各数字から各数字への変更コストの最小値をわーシャルフロイドで計算する

"""



for i in range(10):

    for j in range(10):

        for k in range(9,-1,-1):

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



cost = 0

for i in range(H):

    for j in range(W):

        default = A[i][j]

        if default != -1:

            cost += dp[default][1]



print(cost)

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

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

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



dp = [float("INF")]*(10)

dp[1] = 0

cnt = 0

while cnt <= 10:

    cnt += 1

    for i in range(10):

        for j in range(10):

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



cost = 0

for i in range(H):

    for j in range(W):

        if A[i][j] != -1:

            cost += dp[A[i][j]]

print(cost)
H, W = list(map(int, input().split()))
dp = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]
"""
各数字から各数字への変更コストの最小値をわーシャルフロイドで計算する
"""
for i in range(10):
    for j in range(10):
        for k in range(9, -1, -1):
            dp[j][k] = min(dp[j][k], dp[j][i] + dp[i][k])
cost = 0
for i in range(H):
    for j in range(W):
        default = A[i][j]
        if default != -1:
            cost += dp[default][1]
print(cost)

H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]
dp = [float("INF")] * (10)
dp[1] = 0
cnt = 0
while cnt <= 10:
    cnt += 1
    for i in range(10):
        for j in range(10):
            dp[j] = min(dp[j], dp[i] + C[j][i])
cost = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            cost += dp[A[i][j]]
print(cost)

-dp = [list(map(int, input().split())) for _ in range(10)] +C = [list(map(int, input().split())) for _ in range(10)] -""" -各数字から各数字への変更コストの最小値をわーシャルフロイドで計算する -""" -for i in range(10): -    for j in range(10): -        for k in range(9, -1, -1): -            dp[j][k] = min(dp[j][k], dp[j][i] + dp[i][k]) +dp = [float("INF")] * (10) +dp[1] = 0 +cnt = 0 +while cnt <= 10: +    cnt += 1 +    for i in range(10): +        for j in range(10): +            dp[j] = min(dp[j], dp[i] + C[j][i]) -        default = A[i][j] -        if default != -1: -            cost += dp[default][1] +        if A[i][j] != -1: +            cost += dp[A[i][j]]