def main():

    import sys

    input = sys.stdin.buffer.readline

    H, W = (int(i) for i in input().split())

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

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

    import numpy as np

    from scipy.sparse import csr_matrix

    from scipy.sparse.csgraph import dijkstra

    csr = csr_matrix(c, dtype=np.int32)

    ret = dijkstra(csr)

    ans = 0

    for h in range(H):

        for w in range(W):

            if A[h][w] == -1:

                continue

            cur = A[h][w]

            ans += int(ret[cur][1])

    print(ans)





if __name__ == '__main__':

    main()

def main():

    import sys

    input = sys.stdin.buffer.readline

    H, W = (int(i) for i in input().split())

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

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

    from scipy.sparse.csgraph import dijkstra

    from scipy.sparse import csr_matrix

    d = dijkstra(csr_matrix(c))

    ans = 0

    for h in range(H):

        for w in range(W):

            cur = A[h][w]

            if cur == -1:

                continue

            ans += d[cur][1]

    print((int(ans)))





if __name__ == '__main__':

    main()

def main():
    import sys

    input = sys.stdin.buffer.readline
    H, W = (int(i) for i in input().split())
    c = [[int(i) for i in input().split()] for j in range(10)]
    A = [[int(i) for i in input().split()] for j in range(H)]
    import numpy as np
    from scipy.sparse import csr_matrix
    from scipy.sparse.csgraph import dijkstra

    csr = csr_matrix(c, dtype=np.int32)
    ret = dijkstra(csr)
    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == -1:
                continue
            cur = A[h][w]
            ans += int(ret[cur][1])
    print(ans)


if __name__ == "__main__":
    main()

def main():
    import sys

    input = sys.stdin.buffer.readline
    H, W = (int(i) for i in input().split())
    c = [[int(i) for i in input().split()] for j in range(10)]
    A = [[int(i) for i in input().split()] for j in range(H)]
    from scipy.sparse.csgraph import dijkstra
    from scipy.sparse import csr_matrix

    d = dijkstra(csr_matrix(c))
    ans = 0
    for h in range(H):
        for w in range(W):
            cur = A[h][w]
            if cur == -1:
                continue
            ans += d[cur][1]
    print((int(ans)))


if __name__ == "__main__":
    main()

-    import numpy as np +    from scipy.sparse.csgraph import dijkstra -    from scipy.sparse.csgraph import dijkstra -    csr = csr_matrix(c, dtype=np.int32) -    ret = dijkstra(csr) +    d = dijkstra(csr_matrix(c)) -            if A[h][w] == -1: +            cur = A[h][w] +            if cur == -1: -            cur = A[h][w] -            ans += int(ret[cur][1]) -    print(ans) +            ans += d[cur][1] +    print((int(ans)))