import sys

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, csgraph_from_dense



from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, csgraph_from_dense

# [[10 ** 9, 1, 2],

#  [3, 10 ** 9, 2]

#  [10 ** 9, 4, 7]]





input = sys.stdin.readline





def main():

    H, W = [int(x) for x in input().split()]

    edge = [[int(x) for x in input().split()] for _ in range(10)]

    A = [[int(x) for x in input().split()] for _ in range(H)]



    G = csgraph_from_dense(edge, null_value=0)

    d = floyd_warshall(G)



    ans = 0

    for a in A:

        for v in a:

            if v == -1:

                continue

            ans += d[v][1]



    print((int(ans)))





if __name__ == '__main__':

    main()



from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, csgraph_from_dense



H, W = [int(x) for x in input().split()]

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

A = [[int(x) for x in input().split()] for _ in range(H)]





G = csgraph_from_dense(C, null_value=0)



d = floyd_warshall(G)



ans = 0



for i in range(H):

    for j in range(W):

        if A[i][j] == 1 or A[i][j] == -1:

            continue

        ans += d[A[i][j]][1]





print((int(ans)))





import sys
from scipy.sparse.csgraph import (
    shortest_path,
    floyd_warshall,
    dijkstra,
    bellman_ford,
    johnson,
    csgraph_from_dense,
)
from scipy.sparse.csgraph import (
    shortest_path,
    floyd_warshall,
    dijkstra,
    bellman_ford,
    johnson,
    csgraph_from_dense,
)

# [[10 ** 9, 1, 2],
#  [3, 10 ** 9, 2]
#  [10 ** 9, 4, 7]]
input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]
    edge = [[int(x) for x in input().split()] for _ in range(10)]
    A = [[int(x) for x in input().split()] for _ in range(H)]
    G = csgraph_from_dense(edge, null_value=0)
    d = floyd_warshall(G)
    ans = 0
    for a in A:
        for v in a:
            if v == -1:
                continue
            ans += d[v][1]
    print((int(ans)))


if __name__ == "__main__":
    main()

from scipy.sparse.csgraph import (
    shortest_path,
    floyd_warshall,
    dijkstra,
    bellman_ford,
    johnson,
    csgraph_from_dense,
)

H, W = [int(x) for x in input().split()]
C = [[int(x) for x in input().split()] for _ in range(10)]
A = [[int(x) for x in input().split()] for _ in range(H)]
G = csgraph_from_dense(C, null_value=0)
d = floyd_warshall(G)
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == 1 or A[i][j] == -1:
            continue
        ans += d[A[i][j]][1]
print((int(ans)))

-import sys -from scipy.sparse.csgraph import ( -    shortest_path, -    floyd_warshall, -    dijkstra, -    bellman_ford, -    johnson, -    csgraph_from_dense, -) -# [[10 ** 9, 1, 2], -#  [3, 10 ** 9, 2] -#  [10 ** 9, 4, 7]] -input = sys.stdin.readline - - -def main(): -    H, W = [int(x) for x in input().split()] -    edge = [[int(x) for x in input().split()] for _ in range(10)] -    A = [[int(x) for x in input().split()] for _ in range(H)] -    G = csgraph_from_dense(edge, null_value=0) -    d = floyd_warshall(G) -    ans = 0 -    for a in A: -        for v in a: -            if v == -1: -                continue -            ans += d[v][1] -    print((int(ans))) - - -if __name__ == "__main__": -    main() +H, W = [int(x) for x in input().split()] +C = [[int(x) for x in input().split()] for _ in range(10)] +A = [[int(x) for x in input().split()] for _ in range(H)] +G = csgraph_from_dense(C, null_value=0) +d = floyd_warshall(G) +ans = 0 +for i in range(H): +    for j in range(W): +        if A[i][j] == 1 or A[i][j] == -1: +            continue +        ans += d[A[i][j]][1] +print((int(ans)))