from sys import stdin, setrecursionlimit



setrecursionlimit(10 ** 9)

INF = 1 << 60





def input():

    return stdin.readline().strip()





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

    

    for k in range(10):

        for i in range(10):

            for j in range(10):

                if C[i][j] > C[i][k] + C[k][j]:

                    C[i][j] = C[i][k] + C[k][j]

                    

    ans = 0

    for row in A:

        for n in row:

            if n != -1:

                ans += C[n][1]

    

    print(ans)

    return





if __name__ == '__main__':

    main()

import sys

from collections import Counter



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60





def main():

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

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

    A = list(map(int, read().split()))



    counter = Counter(A)



    for k in range(10):

        for i in range(10):

            for j in range(10):

                if C[i][j] > C[i][k] + C[k][j]:

                    C[i][j] = C[i][k] + C[k][j]



    ans = 0

    for n, freq in list(counter.items()):

        if n not in (-1, 1):

            ans += C[n][1] * freq



    print(ans)

    return





if __name__ == '__main__':

    main()

from sys import stdin, setrecursionlimit

setrecursionlimit(10**9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


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)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if C[i][j] > C[i][k] + C[k][j]:
                    C[i][j] = C[i][k] + C[k][j]
    ans = 0
    for row in A:
        for n in row:
            if n != -1:
                ans += C[n][1]
    print(ans)
    return


if __name__ == "__main__":
    main()

import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    H, W = list(map(int, readline().split()))
    C = [list(map(int, readline().split())) for _ in range(10)]
    A = list(map(int, read().split()))
    counter = Counter(A)
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if C[i][j] > C[i][k] + C[k][j]:
                    C[i][j] = C[i][k] + C[k][j]
    ans = 0
    for n, freq in list(counter.items()):
        if n not in (-1, 1):
            ans += C[n][1] * freq
    print(ans)
    return


if __name__ == "__main__":
    main()

-from sys import stdin, setrecursionlimit +import sys +from collections import Counter -setrecursionlimit(10**9) +read = sys.stdin.read +readline = sys.stdin.readline +readlines = sys.stdin.readlines +sys.setrecursionlimit(10**9) -def input(): -    return stdin.readline().strip() - - -    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)] +    H, W = list(map(int, readline().split())) +    C = [list(map(int, readline().split())) for _ in range(10)] +    A = list(map(int, read().split())) +    counter = Counter(A) -    for row in A: -        for n in row: -            if n != -1: -                ans += C[n][1] +    for n, freq in list(counter.items()): +        if n not in (-1, 1): +            ans += C[n][1] * freq