import numpy as np

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

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

A = [[int(x) if int(x) >= 0 else 1 for x in input().split()] for _ in range(H)]

from scipy.sparse.csgraph import dijkstra

C = dijkstra(C,directed=True) #有向グラフの経路圧縮（ダイクストラ法）

ans = sum(sum(C[x,1] for x in A))

print((int(ans)))
from collections import Counter

from itertools import product



def main():

    with open(0) as f:

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

        C = [list(map(int, f.readline().split())) for _ in range(10)] 

        A = Counter(list(map(int, f.read().split())))



    r10 = list(range(10))

    for k,i,j in product(r10, r10, r10):

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



    ans = sum(C[key][1]*value for key,value in list(A.items()) if key != -1)

    print(ans)



main() 
import numpy as np

H, W = (int(x) for x in input().split())
C = np.array([[int(x) for x in input().split()] for _ in range(10)])
A = [[int(x) if int(x) >= 0 else 1 for x in input().split()] for _ in range(H)]
from scipy.sparse.csgraph import dijkstra

C = dijkstra(C, directed=True)  # 有向グラフの経路圧縮（ダイクストラ法）
ans = sum(sum(C[x, 1] for x in A))
print((int(ans)))

from collections import Counter
from itertools import product


def main():
    with open(0) as f:
        H, W = list(map(int, f.readline().split()))
        C = [list(map(int, f.readline().split())) for _ in range(10)]
        A = Counter(list(map(int, f.read().split())))
    r10 = list(range(10))
    for k, i, j in product(r10, r10, r10):
        C[i][j] = min(C[i][j], C[i][k] + C[k][j])
    ans = sum(C[key][1] * value for key, value in list(A.items()) if key != -1)
    print(ans)


main()

-import numpy as np +from collections import Counter +from itertools import product -H, W = (int(x) for x in input().split()) -C = np.array([[int(x) for x in input().split()] for _ in range(10)]) -A = [[int(x) if int(x) >= 0 else 1 for x in input().split()] for _ in range(H)] -from scipy.sparse.csgraph import dijkstra -C = dijkstra(C, directed=True)  # 有向グラフの経路圧縮（ダイクストラ法） -ans = sum(sum(C[x, 1] for x in A)) -print((int(ans))) +def main(): +    with open(0) as f: +        H, W = list(map(int, f.readline().split())) +        C = [list(map(int, f.readline().split())) for _ in range(10)] +        A = Counter(list(map(int, f.read().split()))) +    r10 = list(range(10)) +    for k, i, j in product(r10, r10, r10): +        C[i][j] = min(C[i][j], C[i][k] + C[k][j]) +    ans = sum(C[key][1] * value for key, value in list(A.items()) if key != -1) +    print(ans) + + +main()