import sys

import numpy as np

from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import floyd_warshall



def main():

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

    cost = np.array([sys.stdin.readline().split() for _ in range(10)], dtype=np.int64)

    A = np.array(sys.stdin.read().split(), dtype=np.int64)

    

    graph = csr_matrix(cost)

    dist = floyd_warshall(csgraph=graph, directed=True)



    ans = dist[np.absolute(A)].T[1].astype(np.int64).sum()

    print(ans)



if __name__ == "__main__":

    main()

import sys

import numpy as np

from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import floyd_warshall



def main():

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

    ca = np.array(sys.stdin.read().split(), dtype=np.int64)

    cost = ca[:100].reshape(10, 10)

    A = ca[100:]

    

    min_cost = floyd_warshall(csgraph=csr_matrix(cost), directed=True)



    total_cost = min_cost[np.absolute(A)].T[1].astype(np.int64).sum()

    print(total_cost)



if __name__ == "__main__":

    main()

import sys
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall


def main():
    H, W = list(map(int, sys.stdin.readline().split()))
    cost = np.array([sys.stdin.readline().split() for _ in range(10)], dtype=np.int64)
    A = np.array(sys.stdin.read().split(), dtype=np.int64)
    graph = csr_matrix(cost)
    dist = floyd_warshall(csgraph=graph, directed=True)
    ans = dist[np.absolute(A)].T[1].astype(np.int64).sum()
    print(ans)


if __name__ == "__main__":
    main()

import sys
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall


def main():
    H, W = list(map(int, sys.stdin.readline().split()))
    ca = np.array(sys.stdin.read().split(), dtype=np.int64)
    cost = ca[:100].reshape(10, 10)
    A = ca[100:]
    min_cost = floyd_warshall(csgraph=csr_matrix(cost), directed=True)
    total_cost = min_cost[np.absolute(A)].T[1].astype(np.int64).sum()
    print(total_cost)


if __name__ == "__main__":
    main()

-    cost = np.array([sys.stdin.readline().split() for _ in range(10)], dtype=np.int64) -    A = np.array(sys.stdin.read().split(), dtype=np.int64) -    graph = csr_matrix(cost) -    dist = floyd_warshall(csgraph=graph, directed=True) -    ans = dist[np.absolute(A)].T[1].astype(np.int64).sum() -    print(ans) +    ca = np.array(sys.stdin.read().split(), dtype=np.int64) +    cost = ca[:100].reshape(10, 10) +    A = ca[100:] +    min_cost = floyd_warshall(csgraph=csr_matrix(cost), directed=True) +    total_cost = min_cost[np.absolute(A)].T[1].astype(np.int64).sum() +    print(total_cost)