# -*- coding: utf-8 -*-



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



for k in range(10):

    for i in range(10):

        for j in range(10):

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



ans = 0

for row in a:

    for ele in row:

        if ele == -1:

            continue

        else:

            ans += c[ele][1]



print(ans)

def main():

    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 = c.copy()



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



    magic = 0

    for row in a:

        for num in row:

            if num == -1:

                continue

            magic += dp[num][1]



    print(magic)





if __name__ == "__main__":

    main()

# -*- coding: utf-8 -*-
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)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for row in a:
    for ele in row:
        if ele == -1:
            continue
        else:
            ans += c[ele][1]
print(ans)

def main():
    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 = c.copy()
    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])
    magic = 0
    for row in a:
        for num in row:
            if num == -1:
                continue
            magic += dp[num][1]
    print(magic)


if __name__ == "__main__":
    main()

-# -*- coding: utf-8 -*- -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)] -for k in range(10): -    for i in range(10): -        for j in range(10): -            c[i][j] = min(c[i][j], c[i][k] + c[k][j]) -ans = 0 -for row in a: -    for ele in row: -        if ele == -1: -            continue -        else: -            ans += c[ele][1] -print(ans) +def main(): +    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 = c.copy() +    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]) +    magic = 0 +    for row in a: +        for num in row: +            if num == -1: +                continue +            magic += dp[num][1] +    print(magic) + + +if __name__ == "__main__": +    main()