Problem:
 Score : 400 points    Problem Statement Joisino the magical girl has decided to turn every single digit that exists on this world into 1 .  Rewriting a digit i with j  (0≤i,j≤9) costs c_{i,j} MP (Magic Points).  She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).  You are given A_{i,j} that describes the square at the i -th row from the top and j -th column from the left, as follows:   If A_{i,j}≠-1 , the square contains a digit A_{i,j} .  If A_{i,j}=-1 , the square does not contain a digit.   Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.      Constraints  1≤H,W≤200  1≤c_{i,j}≤10^3  (i≠j)  c_{i,j}=0  (i=j)  -1≤A_{i,j}≤9  All input values are integers.  There is at least one digit on the wall.         Input Input is given from Standard Input in the following format:  H  W  c_{0,0}  ...  c_{0,9}  :  c_{9,0}  ...  c_{9,9}  A_{1,1}  ...  A_{1,W}  :  A_{H,1}  ...  A_{H,W}       Output Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.        Sample Input 1 2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8      Sample Output 1 12  To turn a single 8 into 1 , it is optimal to first turn 8 into 4 , then turn 4 into 9 , and finally turn 9 into 1 , costing 6 MP.  The wall contains two 8 s, so the minimum total MP required is 6×2=12 .       Sample Input 2 5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1      Sample Output 2 0  Note that she may not need to change any digit.       Sample Input 3 3 5
0 4 3 6 2 7 2 5 3 3
4 0 5 3 7 5 3 7 2 7
5 7 0 7 2 9 3 2 9 1
3 6 2 0 2 4 6 4 2 3
3 5 7 4 0 6 9 7 6 7
9 8 5 2 2 0 4 7 6 5
5 4 6 3 2 3 0 5 4 3
3 6 2 3 4 2 4 0 8 9
4 6 5 4 3 5 3 2 0 8
2 1 3 4 5 7 8 6 4 0
3 5 2 6 1
2 5 3 2 1
6 9 2 5 6      Sample Output 3 47

Solution (Python):
def main():

    h,w = map(int,input().split())
    d = []
    for i in range(10):
        a = list(map(int,input().split()))
        d.append(a)
    def WF(d):
        for k in range(10):
            for i in range(10):
                for j in range(10):
                    d[i][j] = min(d[i][j],d[i][k] + d[k][j])
        return d
    WF(d)
    L = []
    for i in range(h):
        b = list(map(int,input().split()))
        L.append(b)
    ans = 0
    for i in range(h):
        for j in range(w):
            if L[i][j] == 1 or L[i][j] == -1:
                continue
            else:
                ans += d[L[i][j]][1]
    print(int(ans))

if __name__ == '__main__':
	main()

Solution (Python):
def main():
    import numpy as np

    h,w = map(int,input().split())
    d = np.zeros((10,10))
    for i in range(10):
        a = list(map(int,input().split()))
        d[i] = a
    def WF(b):
        for k in range(10):
            for i in range(10):
                for j in range(10):
                    d[i][j] = min(d[i][j],d[i][k] + d[k][j])
        return d
    WF(d)
    L = []
    for i in range(h):
        b = list(map(int,input().split()))
        L.append(b)
    ans = 0
    for i in range(h):
        for j in range(w):
            if L[i][j] == 1 or L[i][j] == -1:
                continue
            else:
                ans += d[L[i][j]][1]
    print(int(ans))

if __name__ == '__main__':
	main()

Solution (Python):

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

h,w = map(int,input().split())
d = np.zeros((10,10))
for i in range(10):
    a = list(map(int,input().split()))
    d[i] = a

def WF(b):
    for k in range(10):
        for i in range(10):
            for j in range(10):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
WF(d)

L = []
for i in range(h):
    b = list(map(int,input().split()))
    L.append(b)
ans = 0
for i in range(h):
    for j in range(w):
        if L[i][j] == 1 or L[i][j] == -1:
            continue
        else:
            ans += d[L[i][j]][1]
print(int(ans))

Solution (Python):
def warshall_floyd(d):
    for k in range(10):
        for i in range(10):
            for j in range(10):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

h,w = map(int,input().split())

d = [[10**4]*10 for i in range(10)] 
for i in range(10):
    a = list(map(int,input().split()))
    for j in range(10):
      d[i][j] = a[j]
for i in range(10):
    d[i][i] = 0
    
ans = 0
chk = tuple(warshall_floyd(d))
for _ in range(h):
  c = list(map(int,input().split()))
  for i in c:
    if i != 1 and i != -1:
      ans += chk[i][1]
      
print(ans)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

n = 10
h,w = myinput()
c = mylistinput(n)
a = mylistinput(h)

d = [[float("inf")]*n for i in range(n)] 


for i in range(n):
    for j in range(n):
        cost = c[i][j]
        d[i][j] = cost


for i in range(n):
    d[i][i] = 0



def warshall_floyd(n,d):
    
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

d = warshall_floyd(n,d)


ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]==-1:
            pass
        else:
            number = a[i][j]
            ans += d[number][1]
print(ans)

Solution (Python):

import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

H,W = imn()

D = [[INF for _ in range(10)] for _ in range(10)]
for i in range(10):
    c = iln()
    for j in range(10):
        D[i][j] = c[j]

for k in range(10):
    for i in range(10):
        for j in range(10):
            D[i][j] = min(D[i][j], D[i][k]+D[k][j])

A = [iln() for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += D[A[i][j]][1]
print(ans)

Solution (Python):
import itertools
from collections import Counter
H, W = map(int, input().split())
cost = []
for i in range(10):
    cost.append(list(map(int, input().split())))
wall = []
for i in range(H):
    wall.append(list(map(int, input().split())))

for k in range(10):
    for i in range(10):
        for j in range(10):
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])

wc = Counter(itertools.chain(*wall))
del wc[-1]
del wc[1]

ans = 0
for k, v in wc.items():
    ans += cost[k][1] * v
print(ans)

Solution (Python):
H,W=map(int,input().split())
c=[list(map(int,input().split())) for _ in range(10)]
A=[list(map(int,input().split())) for _ in range(H)]

to1=[0]*10
for i in range(10):
    dp=[[float("inf") for _ in range(10)] for _ in range(11)]
    dp[0][i]=0
    for j in range(10):
        for k in range(10):
            for l in range(10):
                dp[j+1][k]=min(dp[j+1][k],dp[j][l]+c[l][k])
        
        
    to1[i]=dp[10][1]
    
mi=min(to1)
cnt=0
for i in range(H):
    for j in range(W):
        if A[i][j]==-1:
            cnt=cnt+mi
        else:
            cnt=cnt+to1[A[i][j]]
print(cnt)

Solution (Python):
h,w = map(int,input().split())
ans = 0
c = []
for i in range(10):
    c.append(list(map(int,input().split())))
for _ in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                c[i][j] = min(c[i][j],c[i][k] + c[k][j])
for i in range(h):
    a = list(map(int,input().split()))
    for j in a:
        if j != -1:
            ans += c[j][1]
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

h, w = map(int, readline().split())
memo_graph = [list(map(int, readline().split())) for _ in range(10)]
cost = floyd_warshall(csr_matrix(memo_graph))
ans = 0
for i in range(h):
    for a in list(map(int, readline().split())):
        if a != -1:
            ans += cost[a][1]
print(int(ans))

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq


def warshall_floyd(n):
  for k in range(n):
    for i in range(n):
      for j in range(n):
        if(d[i][k] == INF or d[k][j] == INF):
          pass
        else:
          d[i][j] = min(d[i][j], d[i][k] + d[k][j])
  return








INF = 1001001
H, W = map(int, input().split())
ans = 0

d = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]





warshall_floyd(10)

for i in range(H):
  for j in range(W):
    if(A[i][j] != -1):
      ans += d[A[i][j]][1]

print(ans)

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import dijkstra
from collections import Counter

H, W = map(int, input().split())
C = np.array([tuple(map(int, input().split())) for _ in range(10)])
A = Counter()
for _ in range(H):
    A.update(map(int, input().split()))
try:
    del A[-1]
except:
    pass

way = dijkstra(C.T, indices=1).astype(int)
print(sum(way[k] * v for k, v in A.items()))

Solution (Python):
H,W = map(int,input().split())
d = []
for i in range(10):
  d.append(list(map(int,input().split())))
la = []
for i in range(H):
  la.append(list(map(int,input().split())))

for k in range(10):
  for s in range(10):
    for g in range(10):
      d[s][g] = min(d[s][g],d[s][k]+d[k][g])

ans = 0
for i in range(H):
  for j in range(W):
    a = la[i][j]
    if a != -1:
      ans += d[a][1]
print(ans)

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

h, w = map(int, input().split())
ma = [ list(map(int,input().split(" "))) for i in range(10)]
sco = [ list(map(int,input().split(" "))) for i in range(h)]
a = csr_matrix(ma)
b = shortest_path(a)
ans = 0
for i in range(h):
  for j in range(w):
    num = sco[i][j]
    if num != -1:
      ans += b[num][1]
print(int(ans))

Solution (Python):
import sys
from collections import Counter
from math import isinf


def warshall_floyd(matrix):
    """ ワーシャルフロイド
    :param matrix:  隣接行列(到達不能はfloat("inf"))
    :return matrix
    """
    
    
    V = len(matrix)  
    for i in range(V):
        for j, c2 in enumerate(row[i] for row in matrix):
            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):
                if c1 > c2+c3:
                    matrix[j][k] = c2+c3  
    return matrix


def init_matrix(V: int):
    """:param V: 頂点数
    """
    return [[float('inf')]*V for _ in range(V)]


H, W = map(int, input().split())
V = 10
matrix = init_matrix(V)
for i in range(10):
    c = map(int, input().split())
    for j, cj in enumerate(c):
        matrix[i][j] = cj

warshall_floyd(matrix)
costs = [matrix[i][1] for i in range(10)]

l = []
for _ in range(H):
    l.extend(list(map(int, input().split())))
c = Counter(l)
ans = 0


for k, v in c.items():
    if k == -1:
        continue
    ans += costs[k]*v
print(ans)

Solution (Python):
if __name__ == '__main__':
    h, w = map(int, input().split())
    cost_graph = []
    field = []
    for i in range(10):
        cost_graph.append(list(map(int, input().split())))

    for i in range(h):
        field.append(list(map(int, input().split())))

    for k in range(10):
        for i in range(10):
            for j in range(10):
                cost_graph[i][j] = min(cost_graph[i][j], cost_graph[i][k] + cost_graph[k][j])

    ans = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] == -1 or field[y][x] == 1:
                pass
            else:
                ans += cost_graph[field[y][x]][1]

    print(ans)

Solution (Python):
h,w = map(int,input().split())
l = [list(map(int,input().split())) for i in range(10)]
a = [list(map(int,input().split())) for i in range(h)]
ans = 0
for k in range(10):
    for i in range(10):
        for j in range(10):
            if l[i][j] > l[i][k]+l[k][j]:
                l[i][j] = l[i][k]+l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)

Solution (Python):
h,w = map(int,input().split())
l = [list(map(int,input().split())) for i in range(10)]
a = [list(map(int,input().split())) for i in range(h)]
ans = 0
for rep in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if l[i][j] > l[i][k]+l[k][j]:
                    l[i][j] = l[i][k]+l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)

Solution (Python):
import numpy as np
h,w = map(int,input().split())
l = [list(map(int,input().split())) for i in range(10)]
a = [list(map(int,input().split())) for i in range(h)]
ans = 0
l = np.array(l)
for rep in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if l[i][j] > l[i][k]+l[k][j]:
                    l[i][j] = l[i][k]+l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)

Solution (Python):
def main():
    H, W = map(int, input().split())
    c = [list(map(int, input().split())) for _ in range(10)]
    a = [list(map(int, input().split())) for _ in range(H)]

    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])

    ans = 0
    for row in a:
        for cell in row:
            if ~cell:
                ans += c[cell][1]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
H,W=map(int,input().split())
import numpy as np
C=np.array([list(map(int,input().split())) for _ in range(10)])

A=[]
for i in range(H):
  for a in map(int,input().split()):
    if a!=-1:A+=a,

dp=np.array([[None]*10 for _ in range(10)])
for i in range(10):
  for j in range(10):
    dp[i][j]=C[i][j]
    
for _ in range(10): 
  for i in range(10):
    for j in range(10):
      for k in range(0,9+1):
        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j])
            
ans=0
for a in A:ans+=dp[a][1]
print(ans)

Solution (Python):
import collections
h,w = map(int,input().split())
c = []
wall = []


def judge(num):
    koho = collections.deque()
    for i in range(10):
        if dp[i] > dp[num] + c[i][num]:
            koho.append(i)
            dp[i] = dp[num] + c[i][num]
    if koho:
        here = koho.popleft()
        judge(here)



for i in range(10):
    c.append(list(map(int,input().split())))
for i in range(h):
    wall.extend(list(map(int,input().split())))
wall = collections.Counter(wall)
dp = [0] * 10
for i in range(10):
    dp[i] = c[i][1]
for i in range(10):
    judge(i)
ans = 0
wall = list(wall.items())
for i in range(len(wall)):
    if wall[i][0] == -1:
        pass
    else:
        ans += dp[wall[i][0]] * wall[i][1]
print(ans)

Solution (Python):
H,W=map(int,input().split())
import numpy as np
C=np.array([list(map(int,input().split())) for _ in range(10)])


A=[]
for i in range(H):
    for a in map(int,input().split()):
        if a!=-1:
            A+=a,


dp=np.array([[100100]*10 for _ in range(10)])
for i in range(0,10):
    for j in range(0,10):
        dp[i][j]=C[i][j]
        
for _ in range(10):
    for i in range(0,9+1):
        for j in range(0,9+1):
            for k in range(0,9+1):
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j])
            
ans=0

for a in A:
    ans+=dp[a][1]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


def floyd_warshall(dist):
    v = len(dist)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    return dist

H, W = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
floyd_warshall(c)
A = [list(map(int, input().split())) for _ in range(H)]
ans = 0


for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += c[A[i][j]][1]

print(ans)

Solution (Python):
import collections
h,w = map(int,input().split())
c = []
wall = []


def judge(num):
    koho = collections.deque()
    for i in range(10):
        if dp[i] > dp[num] + c[i][num]:
            koho.append(i)
            dp[i] = dp[num] + c[i][num]
    if koho:
        here = koho.popleft()
        judge(here)



for i in range(10):
    c.append(list(map(int,input().split())))
for i in range(h):
    wall.extend(list(map(int,input().split())))
wall = collections.Counter(wall)
dp = [0] * 10
for i in range(10):
    dp[i] = c[i][1]
for i in range(10):
    judge(i)
ans = 0
wall = list(wall.items())
for i in range(len(wall)):
    if wall[i][0] == -1:
        pass
    else:
        ans += dp[wall[i][0]] * wall[i][1]
print(ans)

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall, csgraph_from_dense
H, W = map(int, input().split())
c = [tuple(map(int, input().split())) for i in range(10)]
A = [list(map(int, input().split())) for i in range(H)]
G = csgraph_from_dense(c)
C = floyd_warshall(G)

ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] >= 0:
            ans += int(C[A[i][j]][1])

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


def floyd_warshall(dist):
    v = len(dist)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

H, W = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
floyd_warshall(c)
A = [list(map(int, input().split())) for _ in range(H)]
ans = 0


for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += c[A[i][j]][1]

print(ans)

Solution (Python):
from collections import Counter
import sys
from math import isinf


def warshall_floyd(v_count: int):
    """ ワーシャルフロイド　(user:htkb)
    :param v_count: 頂点数
    :param matrix:  隣接行列(到達不能はfloat("inf"))
    """
    
    
    global matrix
    for i in range(v_count):
        for j, c2 in enumerate(row[i] for row in matrix):
            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):
                if c1 > c2+c3:
                    matrix[j][k] = c2+c3  


H, W = map(int, input().split())
V = 10
matrix = [[float('inf')]*V for _ in range(V)]
for i in range(10):
    c = map(int, input().split())
    for j, cj in enumerate(c):
        matrix[i][j] = cj

warshall_floyd(V)
costs = [matrix[i][1] for i in range(10)]

l = []
for _ in range(H):
    l.extend(list(map(int, input().split())))
c = Counter(l)
ans = 0


for k, v in c.items():
    if k == -1:
        continue
    ans += costs[k]*v
print(ans)

Solution (Python):
from collections import Counter
import sys
from math import isinf


def warshall_floyd(v_count: int):
    """ ワーシャルフロイド　(user:htkb)
    :param v_count: 頂点数
    :param matrix:  隣接行列(到達不能はfloat("inf"))
    """
    
    
    global matrix
    for i in range(v_count):
        for j, c2 in enumerate(row[i] for row in matrix):
            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):
                if c1 > c2+c3:
                    matrix[j][k] = c2+c3  


H, W = map(int, input().split())
V = 10
matrix = [[float('inf')]*V for _ in range(V)]
for i in range(10):
    c = map(int, input().split())
    for j, cj in enumerate(c):
        matrix[i][j] = cj

warshall_floyd(V)
costs = [matrix[i][1] for i in range(10)]

l = []
for _ in range(H):
    l.extend(list(map(int, input().split())))
c = Counter(l)
ans = 0


for k, v in c.items():
    if k == -1:
        continue
    ans += costs[k]*v
print(ans)

Solution (Python):
def warshall_floyd(d):
    for k in range(10):
        for i in range(10):
            for j in range(10):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

h , w = map(int,input().split())
x = []
for i in range(10):
    p = list(map(int,input().split()))
    x.append(p)

    
x = warshall_floyd(x)
ans = 0
for i in range(h):
    s = list(map(int,input().split()))
    for j in range(w):
        if s[j] != -1:
            ans += x[s[j]][1]
print(ans)

Solution (Python):

h,w = list(map(int,input().split()))
c = [list(map(int,input().split())) for i in range(10)]
a = [list(map(int,input().split())) for i in range(h)]

def warshallFloyd(num_v,am):
    """
    ワーシャルフロイド。All Pairs Shortest Path. APSP
    O(|V|^3)
    Parameters
    ----------
    num_v : int
        Number of nodes
    am : list[list[int]]
        adjacency matrix

    Returns
    -------
    list[list[int]]
        matrix of APSP

    """
    from math import isinf
    from copy import copy
    ret = copy(am)
    for k in range(num_v):
        for i in range(num_v):
            if isinf(ret[i][k]):
                continue
            for j in range(num_v):
                if isinf(ret[k][j]):
                    continue
                ret[i][j]=min(ret[i][j],ret[i][k]+ret[k][j])
    return ret



wrsh_matrix = warshallFloyd(10,c)
ret = 0
for i in a:
    for j in i:
        if j < 0:
            continue
        ret += wrsh_matrix[j][1]
print(ret)

Solution (Python):
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()

Solution (Python):
h,w = map(int,input().split())
c = [list(map(int,input().split())) for i in range(10)]
for i in range(10):
    for j in range(10):
        for k in range(10):
            c[j][k] = min(c[j][k],c[j][i]+c[i][k])
a = [list(map(int,input().split())) for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1:continue
        ans += abs(c[a[i][j]][1])
print(ans)

Solution (Python):
H, W = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]

for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])

ans = 0
for h in range(H):
    for w in range(W):
        a = A[h][w]
        if a != -1:
            ans += C[a][1]
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

def func(fr, to, ignore):
    debug(fr, to, ignore)
    if fr == to:
        return 0
    res = c[fr][to]
    for md in range(10):
        if md in ignore:
            continue
        nignore = deepcopy(ignore)
        nignore.append(md)
        if tmp[fr][md] < inf:
            fr_md = tmp[fr][md]
        else:
            fr_md = func(fr, md, nignore)
        if tmp[md][to] < inf:
            md_to = tmp[md][to]
        else:
            md_to = func(md, to, nignore)
        debug(fr, md, to, fr_md, md_to)
        res = min(res, fr_md + md_to)
    tmp[fr][to] = res
    return res

h, w = MAP()
c = [LIST() for _ in range(10)]
a = [LIST() for _ in range(h)]

d = [[inf for _ in range(10)] for _ in range(10)]
tmp = [[inf for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        debug('a', i, j)
        d[i][j] = func(i, j, [i, j])
for i in range(10):
    debug(d[i])
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1:
            continue
        ans += d[a[i][j]][1]
        debug(d[a[i][j]][1])
print(ans)

Solution (Python):
def examA():
    O = SI()
    E = SI()
    ans = ""
    for i in range(len(O)+len(E)):
        if i%2==0:
            ans += O[i//2]
        else:
            ans += E[i//2]
    print(ans)
    return

def examB():
    A = LI()
    A.sort()
    ans = (A[2] - A[0]) // 2 + (A[2] - A[1]) // 2
    if (A[2]-A[0])%2==1:
        ans += 1
        if (A[2]-A[1])%2==0:
            ans += 1
    elif (A[2]-A[1])%2==1:
        ans += 2
    print(ans)
    return

def examC():
    S = SI()
    S.replace("RL","A")
    S.replace("LR","B")
    N = len(S)
    A = [0]*N
    print(S)
    l = 0; r = 0
    for i,s in enumerate(S):
        if s=="A":
            A[i] += r
        elif s=="B":
            A[i] += l
    ans = 0
    print(ans)
    return

def examD():
    def warshall_floyd(n, d):
        
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        return d
    H, W = LI()
    C = [LI()for _ in range(10)]
    A = [LI()for _ in range(H)]
    C = warshall_floyd(10,C)
    ans = 0
    for i in range(H):
        for j in range(W):
            a = A[i][j]
            if a==-1:
                continue
            ans += C[a][1]
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD()

"""

"""

Solution (Python):
import sys
input = sys.stdin.readline

def floyd_warshall(dist):
    v = len(dist)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

H, W = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
floyd_warshall(c)
A = [list(map(int, input().split())) for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += c[A[i][j]][1]
print(ans)

Solution (Python):
h,w=map(int,input().split())
c=[list(map(int,input().split())) for i in range(10)]
a=[list(map(int,input().split())) for i in range(h)]

for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j]=min(c[i][j],c[i][k]+c[k][j])

ans=0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans+=c[a[i][j]][1]
print(ans)

Solution (Python):
h,w=map(int,input().split())
c=[]
a=[]
b=0
for i in range(10):
    c.append(list(map(int,input().split())))
for i in range(h):
    a.append(list(map(int,input().split())))
for l in range(3):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                c[i][j]=min(c[i][j],c[i][k]+c[k][j])
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            b+=c[a[i][j]][1]
print(b)

Solution (Python):
from scipy.sparse.csgraph import dijkstra
h, w = map(int, input().split())
c = [ list(map(int, input().split())) for _ in range(10) ]
D = dijkstra(c)
C = D[:, 1].astype(int).tolist()
ans = 0
for _ in range(h):
  for i in map(int, input().split()):
    if i != -1:
      ans += C[i]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(10)]
for k in range(10):
  for i in range(10):
    for j in range(10):
      d[i][j] = min(d[i][j], d[i][k] + d[k][j])
res = 0
for _ in range(H):
  a = list(map(int, input().split()))
  for x in a:
    if x != -1: res += d[x][1]
print(res)

Solution (Python):
def main():
    h, w = map(int, input().split())
    cost = [list(map(int, input().split())) for _ in range(10)]
    a = [list(map(int, input().split())) for _ in range(h)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    answer = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] == -1:
                continue
            answer += cost[a[i][j]][1]
    print(answer)


if __name__ == '__main__':
    main()