import numpy as np

from scipy.sparse.csgraph import dijkstra

from collections import defaultdict



import sys

input = sys.stdin.readline



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

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



A=[]

for _ in range(H):

  A+=[int(x) for x in input().split()] 



dd = defaultdict(int)

for key in A:

  if key!=1 and key!=-1:

    dd[key] += 1



res=0

for k, v in list(dd.items()):

  res+= v*int(dijkstra(c,indices = k)[1])

print(res)
import numpy as np

from scipy.sparse.csgraph import dijkstra

from collections import defaultdict



import sys

input = sys.stdin.readline



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

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



A=[]

for _ in range(H):

  A+=[int(x) for x in input().split()] 



dd = defaultdict(int)

for key in A:

  if key!=1 and key!=-1:

    dd[key] += 1



res=0

scores=dijkstra(c.T,indices = 1).astype(int)

for k, v in list(dd.items()):

  res+= v*scores[k]

print(res)
import numpy as np
from scipy.sparse.csgraph import dijkstra
from collections import defaultdict
import sys

input = sys.stdin.readline
[H, W] = [int(x) for x in input().split()]
c = np.array([[int(x) for x in input().split()] for _ in range(10)])
A = []
for _ in range(H):
    A += [int(x) for x in input().split()]
dd = defaultdict(int)
for key in A:
    if key != 1 and key != -1:
        dd[key] += 1
res = 0
for k, v in list(dd.items()):
    res += v * int(dijkstra(c, indices=k)[1])
print(res)

import numpy as np
from scipy.sparse.csgraph import dijkstra
from collections import defaultdict
import sys

input = sys.stdin.readline
[H, W] = [int(x) for x in input().split()]
c = np.array([[int(x) for x in input().split()] for _ in range(10)])
A = []
for _ in range(H):
    A += [int(x) for x in input().split()]
dd = defaultdict(int)
for key in A:
    if key != 1 and key != -1:
        dd[key] += 1
res = 0
scores = dijkstra(c.T, indices=1).astype(int)
for k, v in list(dd.items()):
    res += v * scores[k]
print(res)

+scores = dijkstra(c.T, indices=1).astype(int) -    res += v * int(dijkstra(c, indices=k)[1]) +    res += v * scores[k]