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):
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)


input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])

    def search(self, s, g):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]



def solve():
    h, w = getList()
    cost = []
    for i in range(10):
        cost.append(getList())

    V = 10
    for k in range(V):
        for i in range(V):
            for j in range(V):
                if cost[i][k] != INF and cost[k][j] != INF:
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])

    wall = []
    ans = 0
    for i in range(h):
        for num in getList():
            if num != -1:
                ans += cost[num][1]
    print(ans)
    
    return
def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

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

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 row in A:
    for a in row:
        if a == -1: continue
        ans += C[a][1]
print(ans)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

h, w = MAP()
c = []
for i in range(10):
    c.append(LIST())
a = []
for i in range(h):
    a.append(LIST())

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])

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 math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

h, w = MAP()
c = []
for i in range(10):
    c.append(LIST())
a = []
for i in range(h):
    a.append(LIST())

chk = True
while chk:
    chk = False
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if c[i][j] > c[i][k]+c[k][j]:
                    c[i][j] = c[i][k]+c[k][j]
                    chk = True

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 _ in range(10)]
a=[list(map(int,input().split())) for _ in range(h)]

dist=[]
dist.extend(c)
for i in range(10):
  for j in range(10):
    for k in range(10):
      if dist[j][k]>dist[j][i]+dist[i][k]:
        dist[j][k]=dist[j][i]+dist[i][k]
print(sum(dist[a[i][j]][1] for i in range(h) for j in range(w) if a[i][j]>-1))

Solution (Python):
icase=0
if icase==0:
    h,w=map(int,input().split())
    dp=[[0]*10 for i in range(10)]
    for i in range(10):
        dp[i]=list(map(int,input().split()))
    a=[]
    for i in range(h):
        a.extend(list(map(int,input().split())))
elif icase==1:
    h,w=2,4
    dp=[[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]]

    a=[-1, -1, -1, -1, 8, 1, 1, 8]

from collections import Counter
ac=Counter(a)

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

icnt=0
for i in range(10):
    if ac[i]>0:
        icnt+=dp[i][1]*ac[i]  

print(icnt)

Solution (Python):
def main():
    inf=float("inf")
    h,w=map(int,input().split())
    lst=[list(map(int,input().split())) for _ in range(10)]
    dp1=[[inf]*10 for _ in range(10)]
    
    for i in range(10):
        for j in range(10):
            dp1[i][j]=lst[i][j]
    
    for k in range(1,11):
        dp2=[dp1[i][:] for i in range(10)]
        for i in range(10):
            for j in range(10):
                if dp2[i][j]>dp2[i][k-1]+dp2[k-1][j]:
                    dp1[i][j]=dp2[i][k-1]+dp2[k-1][j]

    wall=[list(map(int,input().split())) for _ in range(h)]
    sm=0
    for i in range(h):
        for j in range(w):
            if wall[i][j]==-1 : continue
            sm+=dp1[wall[i][j]][1]

    print(sm)
    
main()

Solution (Python):
import sys
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
import numpy as np

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W = map(int, readline().split())
    L = list(list(map(int, readline().split())) for _ in range(10))
    A = list(list(map(int, readline().split())) for _ in range(H))
    dist = dijkstra(csr_matrix(L))
    ans = 0
    for a in A:
        for i in a:
            if i == -1:
                continue
            else:
                ans += dist[i][1]
    print(int(ans))




if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

H, W = map(int, input().split())

V = 10

dists = []

for _ in range(V):
    dists.append(list(map(int, input().split())))

count = defaultdict(int)

for _ in range(H):
    for i in map(int, input().split()):
        count[i] += 1

for k in range(V):
    for i in range(V):
        for j in range(V):
            dists[i][j] = min(dists[i][j], dists[i][k] + dists[k][j])


ans = 0

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

Solution (Python):
def main():
    import sys
    from heapq import heappop, heappush

    def input(): return sys.stdin.readline().rstrip()

    def dijkstra(s):
        inf = 10**6
        dist = [inf]*10
        dist[1] = 0
        que = [(0, 1)]
        while que:
            cost, node = heappop(que)
            if cost > dist[node]:
                continue
            for i in range(10):
                if i == node:
                    continue
                if dist[i] > dist[node] + g[i][node]:
                    dist[i] = dist[node]+g[i][node]
                    heappush(que, (dist[i], i))
        return dist
    
    h, w = map(int, input().split())
    g = [[] for _ in range(10)]
    for i in range(10):
        g[i] = list(map(int, input().split()))

    from collections import defaultdict
    c_lis = defaultdict(int)
    for i in range(h):
        for j in input().split():
            tmp = int(j)
            if abs(tmp) != 1:
                c_lis[tmp] += 1
    
    dist = dijkstra(1)
    ans = 0
    for key in c_lis:
        ans += c_lis[key]*dist[key]
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
import sys

h,w=map(int,input().split())
costs=[]
for _ in range(10):
  costs.append(list(map(int,input().split())))
dd=defaultdict(lambda:0)
for i in map(int,sys.stdin.read().split()):
  dd[i]+=1
  
min_costs=[0]*10
for y in range(10):
  min_costs[y]=costs[y][1]

for _ in range(100):
  for x in range(10):
    for y in range(10):
      min_costs[y]=min(min_costs[y], min_costs[x]+costs[y][x])

ans=0
for i in range(10):
  ans+=dd[i]*min_costs[i]
print(ans)

Solution (Python):
from collections import defaultdict
import sys

h,w=map(int,input().split())
costs=[]
for _ in range(10):
  costs.append(list(map(int,input().split())))
dd=defaultdict(lambda:0)
for i in map(int,sys.stdin.read().split()):
  dd[i]+=1
  
min_costs=[float("inf")]*10
for y in range(10):
  min_costs[y]=costs[y][1]

for _ in range(100):
  for x in range(10):
    if x==1:
      continue
    for y in range(10):
      min_costs[y]=min(min_costs[y], min_costs[x]+costs[y][x])

ans=0
for i in range(10):
  ans+=dd[i]*min_costs[i]
print(ans)

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

def dijkstra(s):
    hq = [(0, s)]
    heapq.heapify(hq) 
    cost = [float('inf')] * n 
    cost[s] = 0 
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]: 
            continue
        for d, u in e[v]:
            tmp = d + cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    return cost

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

e = [[] for _ in range(n)]

for i in range(n):
    for j in range(n):
        e[i].append((c[i][j], j))
        
mp = float('inf')
mplist = [mp]*n
for i in range(n):
    dist = dijkstra(i)
    mplist[i] = dist[1]

chgmp =0
for ih in range(H):
    for iw in range(W):
        if A[ih][iw]!=-1:
            chgmp += mplist[A[ih][iw]]
print(chgmp)

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 i in range(h):
    for j in range(w):
        if a[i][j] == -1: continue
        ans += c[a[i][j]][1]

print(ans)

Solution (Python):
def warshall_floyd(d):
    
    n = len(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 = map(int, input().split())

c = [[] for _ in range(10)]
a = [[] for _ in range(h)]

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

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

warshall_floyd(c)

ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1 and 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 _ 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][k]+c[k][j], c[i][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):
from heapq import heappush,heappop
inf=100000
h,w=map(int,input().split())
cost=[]
for i in range(10):
    lst=list(map(int,input().split()))
    cost.append(lst)

def dijkstra(start):
    min_cost=[inf]*10
    min_cost[start]=0
    q=[]
    heappush(q,(0,start))
    while q:
        c,v=heappop(q)
        if v==1:
            return c
        if c>min_cost[v]:
            continue
        for i,nc in enumerate(cost[v]):
            if c+nc<min_cost[i]:
                min_cost[i]=c+nc
                heappush(q,(c+nc,i))

to_one=[None]*10
for i in range(10):
    to_one[i]=dijkstra(i)
ans=0
for i in range(h):
    lst=list(map(int,input().split()))
    for j in lst:
        if j!=-1:
            ans+=to_one[j]
print(ans)

Solution (Python):
import heapq
h, w = map(int, input().split())
cs = [[] for _ in range(10)]
g = [[] for _ in range(10)]
for i in range(10):
    lis = list(map(int, input().split()))
    for j in range(10):
        if i == j:
            continue
        g[j].append((i,lis[j]))
ds = [-1]*10
hq = [(0,1)]
while hq:
    c,idx = heapq.heappop(hq)
    if ds[idx] != -1:
        continue
    ds[idx] = c
    for i in g[idx]:
        if ds[i[0]] == -1:
            heapq.heappush(hq,(c+i[1],i[0]))
res = 0
for i in range(h):
    lis = list(map(int, input().split()))
    for j in lis:
        if j != 1 and j != -1:
            res += ds[j]
print(res)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from scipy.sparse.csgraph import floyd_warshall

h,w=nii()
c=[lnii() for i in range(10)]
a=[lnii() for i in range(h)]

s=floyd_warshall(c)

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

Solution (Python):


from collections import defaultdict

H, W = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
for w in range(10):
    for u in range(10):
        for v in range(10):
            if c[u][v] > c[u][w] + c[w][v]:
                c[u][v] = c[u][w] + c[w][v]

cost = 0
for _ in range(H):
    lv = map(int, input().split())
    for v in lv:
        if v >= 0:
            cost += c[v][1]
print(cost)

Solution (Python):
def warshall_floyd(d):
    for i in range(10):
        for j, c2 in enumerate(d[j][i] for j in range(10)):
            for k, (c1,c3) in enumerate(zip(d[j], d[i])):
                if c1 > c2+c3:
                    d[j][k] = c2+c3
    return d

h,w = map(int,input().split())
d=[list(map(int,input().split())) for _ in range(10)] 
a=[list(map(int,input().split())) for _ in range(h)]
a_cnt=[0]*10
for i in range(h):
    for j in range(w):
        if a[i][j]==-1:
            continue
        a_cnt[a[i][j]]+=1
d=warshall_floyd(d)
ans=0
for i,j in enumerate(a_cnt):
    ans+=d[i][1]*j
print(ans)

Solution (Python):
INFTY = 10**4+10
H,W = map(int,input().split())
C = [list(map(int,input().split())) for _ in range(10)]
cost = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        cost[i][j] = C[i][j]
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])
A = [list(map(int,input().split())) for _ in range(H)]
tot = 0
for i in range(H):
    for j in range(W):
        if A[i][j]==1 or A[i][j]==-1:continue
        tot += cost[A[i][j]][1]
print(tot)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

from heapq import heappop, heappush
H, W = mapint()
Cs = [list(mapint()) for _ in range(10)]
lis = []
for h in range(H):
    lis.extend(list(mapint()))

def dijkstra(start):
    Q = [(0, start)]
    checked = [0]*10
    while Q:
        d, v = heappop(Q)
        checked[v] = 1
        if v==1:
            return d
        for i in range(10):
            if not checked[i]:
                heappush(Q, (d+Cs[v][i], i))

costs = [0]*10
for i in range(10):
    costs[i] = dijkstra(i)

from collections import Counter
c = Counter(lis)
ans = 0
for i in range(10):
    ans += costs[i]*c[i]
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):
        if A[h][w] == -1:
            continue
        ans += C[A[h][w]][1]

print(ans)

Solution (Python):
f=lambda:[*map(int,input().split())]
H,W=f()
r=range(10)
c=[f() for _ in r]
C=[0]*10

for _ in range(H):
  for i in f():
    if i>=0:
      C[i]+=1

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

print(sum(c[i][1]*C[i] for i in r))

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)]

cost = [float("INF")]*(10)
cost[1] = 0
cnt = 0
while cnt < 10:
    cnt += 1
    for i in range(10):
        for j in range(10):
            cost[j] = min(cost[j],cost[i]+C[j][i])

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

Solution (Python):

from heapq import heappush, heappop
from collections import defaultdict
INF = float("inf")

def dijkstra(C, g):
    '''
    各頂点からg「まで」の最小コストを求める
    C ... 隣接行列で表されたコスト
    g ... 終点
    '''
    n = len(C)
    min_cost = [INF] * n
    min_cost[g] = 0
    visited = defaultdict(bool)
    H = [(0, g)]

    while H:
        v_cost, v = heappop(H)
        if visited[v]: continue
        visited[v] = True
        for u in range(n):
            new_cost = C[u][v] + v_cost
            if new_cost < min_cost[u]:
                min_cost[u] = new_cost
                heappush(H, (new_cost, u))

    return min_cost


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)]

min_cost = dijkstra(C, 1)

ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == -1: continue
        ans += min_cost[A[i][j]]

print(ans)

Solution (Python):
def floyd_warshall(dp, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
    return dp

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)]

c = floyd_warshall(c, 10)

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

print(ans)

Solution (Python):
import sys
from collections import defaultdict
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()


def main():
    H, W = map(int, input().split())
    C = [list(map(int, input().split())) for _ in range(10)]
    """
    備忘録としてワ―シャルフロイドによる実装も試してみる。
    wikipediaの解説が意外とわかりやすい？
    """
    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])
    
    d = defaultdict(int)
    for _ in range(H):
        A = list(map(int, input().split()))
        for a in A: d[a] += 1
    ans = 0
    for i in range(10): ans += C[i][1] * d[i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()


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

    cost = [10**18] * 10
    cost[1] = 0
    q = [(0, 1)]
    while q:
        c, u = heappop(q)
        if c > cost[u]: continue
        for i in range(10):
            if i == u: continue
            if c + C[i][u] < cost[i]:
                cost[i] = cost[u] + C[i][u]
                heappush(q, (cost[i], i))
    
    d = defaultdict(int)
    for _ in range(H):
        A = list(map(int, input().split()))
        for a in A: d[a] += 1
    ans = 0
    for i in range(10): ans += cost[i] * d[i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict, deque, Counter
import math


from bisect import bisect_left, bisect_right
import heapq

sys.setrecursionlimit(1000000)


input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10 ** 9 + 7
divide = lambda x: pow(x, MOD - 2, MOD)

ans = 0
def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n - k])) % MOD

def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret

def kaijyo_kai(n, k):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * (k+i))% MOD)
    return ret


def dijkstra(graph, start, n):
    h = []
    heapq.heappush(h, (0, start))
    costs = [INF for _ in range(n)]
    while h:
        cost, cur = heapq.heappop(h)
        if costs[cur] != INF:
            continue
        costs[cur] = cost
        for edge in graph[cur]:
            ecost, tgt = edge
            if costs[tgt] == INF:
                heapq.heappush(h, (cost + ecost, tgt))

    return costs

def solve():
    n, m = getList()
    graph = [[] for i in range(10)]
    magic = []
    for i in range(10):
        mg = getList()
        for j, mm in enumerate(mg):
            if i != j:
                graph[i].append((mm, j))

    for i in range(10):
        magic.append(dijkstra(graph, i, 10))
    
    


    ans = 0
    for i in range(n):
        for j in getList():
            if j != -1:
                ans += magic[j][1]

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    
    solve()

Solution (Python):
from types import CodeType
import unittest


def solve():
    return 0


def main():
    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):
                if c[i][k] + c[k][j] < c[i][j]:
                    c[i][j] = c[i][k] + c[k][j]
    total = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] == -1:
                continue
            total += c[a[i][j]][1]
    print(total)


if __name__ == "__main__":
    main()


class Test(unittest.TestCase):
    def test_case1(self):
        expected = 0
        actual = solve()
        self.assertEqual(actual, expected)

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 row in A:
    for a in row:
        if a < 0: continue
        ans += C[a][1]
print(ans)

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


for k in range(10):
    for i in range(10):
        for j in range(10):
            if C[i][j] > C[i][k] + C[k][j]:
                C[i][j] = C[i][k] + C[k][j]

from collections import Counter
cnt = Counter()
for __ in range(H):
    
    cnt.update(list(map(int, input().split())))

ans = 0
for i in cnt.keys():
    if i == -1:
        continue
    ans += C[i][1] * cnt[i]

print(ans)

Solution (Python):
h, w = list(map(int, input().split()))
power = [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):
            power[j][k] = min(power[j][i] + power[i][k], power[j][k])

ans = 0
for i in range(h):
    wall = list(map(int, input().split()))
    for item in wall:
        if item < 0:  continue
        ans += power[item][1]
print(ans)

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

number_count = {i: 0 for i in range(10)}
for _ in range(H):
    row = list(map(int, input().split()))
    for num in row:
        if num == -1:
            continue
        number_count[num] += 1

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])


    


result = 0
for k, v in number_count.items():
    result += C[k][1] * v

print(result)

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

def warshall_floyd(c):
    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])

    mn_c=[c[s][1] for s in range(10)]
    return mn_c

mn_c=warshall_floyd(c)

sm=0
for i in range(h):
    a=list(map(int,input().split()))
    for k in a:
        if k>=0:
            sm+=mn_c[k]

print(sm)

Solution (Python):
from itertools import permutations


def warshallfloyd(N, DIST):  
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                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)]
a = [list(map(int, input().split())) for _ in range(h)]

ans, dist = 0, warshallfloyd(10, c)
for ai in a:
    for aij in ai:
        if aij != -1:
            ans += dist[aij][1]
print(ans)

Solution (Python):
from heapq import heappop,heappush

def dijkstra(G, start):
    n = len(G)
    dist = [float("inf")]*n
    visited = set()
    heap = []
    heap.append((0, start))

    while heap and len(visited) != n:
        d, cur = heappop(heap)
        if cur in visited:
            continue
        visited.add(cur)
        dist[cur] = d
        for to in range(10):
            if to not in visited:
                heappush(heap, (d+G[cur][to], to))
    return dist

h,w = map(int, input().split())
c = [list(map(int, input().split())) for i in range(10)]
rc = [[0]*10 for i in range(10)]
for i in range(10):
    for j in range(10):
        rc[i][j] = c[j][i]
a = [list(map(int, input().split())) for i in range(h)]
dist = dijkstra(rc, 1)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1:
            ans += dist[a[i][j]]
print(ans)