import sys

input = sys.stdin.readline

from collections import Counter

from itertools import chain



def readlines(n):

    for _ in range(n):

        yield list(map(int, input().split()))



def main():

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



    C = [[int(c) for c in input().split()] for _ in range(10)]



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



    counter = Counter(chain(*readlines(h)))



    for i in range(10):

        yield C[i][1] * counter[i]





print((sum(main())))





main()
import sys

input = sys.stdin.readline

def readlines(n):

    for _ in range(n):

        yield list(map(int, input().split()))



def main():

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



    C = list(readlines(10))



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



    for line in readlines(h):

        yield sum(C[c][1] for c in line if c != -1) 





print((sum(main())))





main()
import sys

input = sys.stdin.readline
from collections import Counter
from itertools import chain


def readlines(n):
    for _ in range(n):
        yield list(map(int, input().split()))


def main():
    h, w = list(map(int, input().split()))
    C = [[int(c) for c in input().split()] for _ in range(10)]
    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])
    counter = Counter(chain(*readlines(h)))
    for i in range(10):
        yield C[i][1] * counter[i]


print((sum(main())))
main()

import sys

input = sys.stdin.readline


def readlines(n):
    for _ in range(n):
        yield list(map(int, input().split()))


def main():
    h, w = list(map(int, input().split()))
    C = list(readlines(10))
    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])
    for line in readlines(h):
        yield sum(C[c][1] for c in line if c != -1)


print((sum(main())))
main()

-from collections import Counter -from itertools import chain -    C = [[int(c) for c in input().split()] for _ in range(10)] +    C = list(readlines(10)) -    counter = Counter(chain(*readlines(h))) -    for i in range(10): -        yield C[i][1] * counter[i] +    for line in readlines(h): +        yield sum(C[c][1] for c in line if c != -1)