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

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

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



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

cost[1] = 0

cnt = 0

while cnt < 10:

    cnt += 1

    for i in range(10):

        for j in range(10):

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



ans = 0

for i in range(H):

    for j in range(W):

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

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

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

-dp = [float("INF")] * (10) -dp[1] = 0 +cost = [float("INF")] * (10) +cost[1] = 0 -while cnt <= 10: +while cnt < 10: -            dp[j] = min(dp[j], dp[i] + C[j][i]) -cost = 0 +            cost[j] = min(cost[j], cost[i] + C[j][i]) +ans = 0 -            cost += dp[A[i][j]] -print(cost) +            ans += cost[A[i][j]] +print(ans)