import copy



def main():

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

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

    a = [[int(i) for i in input().split()] for i in range(H)]

    #d = copy.deepcopy(c)



    # ワーシャルフロイド法

    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 i in a:

        for j in i:

            if j > 1 or j == 0:

                ans += c[j][1]

    

    print(ans)





if __name__ == "__main__":

    main()

import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines



def main():

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

    C = [[int(i) for i in readline().split()] for _ in range(10)]

    A = [[int(i) for i in readline().split()] for _ in range(H)]



    # 頂点kを経由する場合としない場合を比較

    for k in range(10):

        for i in range(10):

            for j in range(10):

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

    

    ans = 0

    for a in A:

        for i in a:

            if i != -1 and i != 1:

                ans += C[i][1]



    print(ans)





if __name__ == "__main__":

    main()

import copy


def main():
    H, W = list(map(int, input().split()))
    c = [[int(i) for i in input().split()] for i in range(10)]
    a = [[int(i) for i in input().split()] for i in range(H)]
    # d = copy.deepcopy(c)
    # ワーシャルフロイド法
    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 i in a:
        for j in i:
            if j > 1 or j == 0:
                ans += c[j][1]
    print(ans)


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def main():
    H, W = list(map(int, readline().split()))
    C = [[int(i) for i in readline().split()] for _ in range(10)]
    A = [[int(i) for i in readline().split()] for _ in range(H)]
    # 頂点kを経由する場合としない場合を比較
    for k in range(10):
        for i in range(10):
            for j in range(10):
                C[i][j] = min(C[i][k] + C[k][j], C[i][j])
    ans = 0
    for a in A:
        for i in a:
            if i != -1 and i != 1:
                ans += C[i][1]
    print(ans)


if __name__ == "__main__":
    main()

-import copy +import sys + +read = sys.stdin.read +readline = sys.stdin.readline +readlines = sys.stdin.readlines -    H, W = list(map(int, input().split())) -    c = [[int(i) for i in input().split()] for i in range(10)] -    a = [[int(i) for i in input().split()] for i in range(H)] -    # d = copy.deepcopy(c) -    # ワーシャルフロイド法 +    H, W = list(map(int, readline().split())) +    C = [[int(i) for i in readline().split()] for _ in range(10)] +    A = [[int(i) for i in readline().split()] for _ in range(H)] +    # 頂点kを経由する場合としない場合を比較 -                c[i][j] = min(c[i][j], c[i][k] + c[k][j]) +                C[i][j] = min(C[i][k] + C[k][j], C[i][j]) -    for i in a: -        for j in i: -            if j > 1 or j == 0: -                ans += c[j][1] +    for a in A: +        for i in a: +            if i != -1 and i != 1: +                ans += C[i][1]