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):
H, W = map(int, input().split())
dp = [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):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

cost = 0
for a in A:
    for i in a:
        if i > -1:
            cost += dp[i][1]
print(cost)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,m=LI()
  d=[]
  for _ in range(10):
    d.append(LI())

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

  ans=0
  for _ in range(n):
    l=LI()
    for x in l:
      if x not in (1,-1):
        ans+=d[x][1]

  return ans


print(main())

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


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

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

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

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


d=copy.copy(c)

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

ans=0

for hi in range(h):
    for ahi in a[hi]:
        if(ahi != -1):
            ans+=d[ahi][1]
print(ans)

Solution (Python):
from collections import deque
import numpy as np
H, W = map(int, input().split())
c = np.array([list(map(int, input().split())) for i in range(10)])
A = [list(map(int,input().split())) for i in range(H)]
d = c[:, 1]
d[1] = 0
ans = 0
for i in range(10):
    if i == 1:
        continue
    q = deque([[i, 0]])
    while len(q) > 0:
        p = q.popleft()
        d[i] = min(d[i], p[1] + c[p[0]][1])
        for j in range(10):
            if j == p[0] or j == 1:
                continue
            if p[1] + c[p[0]][j] < d[i]:
                q.append([j, p[1] + c[p[0]][j]])

for i in range(H):
    for j in range(W):
        if A[i][j] >= 0:
            ans += d[A[i][j]]
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)]

import heapq
def dijkstra_heap(s):
    
    d = [float("inf")] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d
n,w = 10,90 
edge = [[] for i in range(n)]

for i in range(10):
    for j in range(10):
        x,y,z = i,j,c[j][i]
        
        edge[y].append([z,x]) 
ans=[]
for i in range(10):
    ans.append(dijkstra_heap(i)[1])
    

A=0
for i in range(h):
    for j in range(W):
        if a[i][j]>=0:
            A+=ans[a[i][j]]
print(A)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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


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


cc = warshall_floyd(c)

ans = 0
for a in A:
    if a == -1:
        continue
    ans += cc[a][1]
print(ans)

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


H, W = map(int, input().split())
cost = [list(map(int, input().split())) for _ in range(10)]
field = [list(map(int, input().split())) for _ in range(H)]

cost_list = Warshall_Floyd(10, cost)
ans = 0

for h in range(H):
    for w in range(W):
        if abs(field[h][w]) == 1:
            continue
        ans += cost_list[field[h][w]][1]

print(ans)

Solution (Python):
import heapq


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

mini_cost_to_1_from = [10**4] * 10
mini_cost_to_1_from[1] = 0

for num in range(10):
    if num == 1:
        continue
    seen = [False] * 10
    todo = []
    for to in range(10):
        if num == to:
            continue
        cost = costs[num][to]
        heapq.heappush(todo, [cost, to])

    while todo:
        cost, node = heapq.heappop(todo)
        if node == 1:
            mini_cost_to_1_from[num] = cost
            break

        if seen[node]:
            continue

        seen[node] = cost

        for to in range(10):
            if node == to:
                continue
            if seen[to]:
                continue
            add = costs[node][to]
            heapq.heappush(todo, [cost+add, to])

num_count = [0] * 11
for i in range(h):
    wall = list(map(int, input().split()))
    for num in wall:
        num_count[num] += 1

ans = 0
for num in range(10):
    ans += num_count[num] * mini_cost_to_1_from[num]

print(ans)

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
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)]

d = floyd_warshall(C)
cnt = 0
for arr in A:
  for i in arr:
    if i != -1:
      cnt += d[i][1]
      
print(int(cnt))

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

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

cost=[C[i][1] for i in range(10)]
cnt=0
for i in range(h):
    for j in range(w):
        if W[i][j]!=-1:
            cnt +=cost[W[i][j]]

print(cnt)

Solution (Python):
H, W = map(int, input().split())
c = [0 for _ in range(10)]
for i in range(10):
  c[i] = list(map(int, input().split()))
A = [0 for _ in range(H)]
for i in range(H):
  A[i] = list(map(int, input().split()))
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):
from scipy.sparse.csgraph import floyd_warshall

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

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

ans = 0
for i in range(H):
    a = list(map(int, input().split()))
    for j in a:
        if j != -1 and j != 1:
            ans += d[j][1]

print(int(ans))

Solution (Python):
import copy
from collections import Counter

def make_table():
    dp = copy.deepcopy(C)

    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


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

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

cntA = Counter(A)

dp = make_table()

print(sum([dp[key][1] * cntA[key] if key != -1 else 0 for key in cntA]))

Solution (Python):
H,W = map(int,input().split())
c = []
dicta = {}
dicta[-1] = 0
for i in range(10):
    c.append(list(map(int,input().split())))
    dicta[i] = 0
c = [i for i in zip(*c)]
for i in range(H):
    a = list(map(int,input().split()))
    for i in a:
        dicta[i] += 1
dicta.pop(-1)
cost = [0 for i in range(10)]
def dijkstra_heap(start,node,cost):
    import heapq
    d = [float('inf') for i in range(node)]
    d[start] = 0
    d2 = [[0,start]]
    heapq.heapify(d2)
    while d2:
        c,v = heapq.heappop(d2)
        if d[v] < c:
            continue
        for j in range(node):
            if d[j] > d[v]+cost[v][j]:
                d[j] = d[v]+cost[v][j]
                heapq.heappush(d2,[d[j],j])
    return d

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

Solution (Python):
H,W = map(int,input().split())
c = []
dicta = {}
dicta[-1] = 0
for i in range(10):
    c.append(list(map(int,input().split())))
    dicta[i] = 0
c = [i for i in zip(*c)]
for i in range(H):
    a = list(map(int,input().split()))
    for i in a:
        dicta[i] += 1
dicta.pop(-1)
cost = [0 for i in range(10)]
def dijkstra_heap(start,node,cost):
    import heapq
    d = [float('inf') for i in range(node)]
    used = [False for i in range(node)]
    d[start] = 0
    d2 = [[0,start]]
    heapq.heapify(d2)
    while d2:
        c,v = heapq.heappop(d2)
        if used[v]:
            continue
        used[v] = True
        for j in range(node):
            d[j] = min(d[j],d[v]+cost[v][j])
            if not used[j]:
                heapq.heappush(d2,[d[j],j])
    return d

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

Solution (Python):
H,W = map(int,input().split())
c = []
dicta = {}
dicta[-1] = 0
for i in range(10):
    c.append(list(map(int,input().split())))
    dicta[i] = 0
c = [i for i in zip(*c)]
for i in range(H):
    a = list(map(int,input().split()))
    for i in a:
        dicta[i] += 1
dicta.pop(-1)
cost = [0 for i in range(10)]
def dijkstra_heap(start,node,cost):
    import heapq
    d = [float('inf') for i in range(node)]
    used = [False for i in range(node)]
    d[start] = 0
    d2 = [[0,start]]
    heapq.heapify(d2)
    while True:
        flag = True
        while d2:
            c,v = heapq.heappop(d2)
            if not used[v]:
                flag = False
                break
        if flag:
            break
        used[v] = True
        for j in range(node):
            d[j] = min(d[j],d[v]+cost[v][j])
            heapq.heappush(d2,[d[j],j])
    return d

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

Solution (Python):
H,W = map(int,input().split())
c = []
dicta = {}
dicta[-1] = 0
for i in range(10):
    c.append(list(map(int,input().split())))
    dicta[i] = 0
c = [i for i in zip(*c)]
for i in range(H):
    a = list(map(int,input().split()))
    for i in a:
        dicta[i] += 1
dicta.pop(-1)
cost = [0 for i in range(10)]
def dijkstra(start,node,cost):
    d = [float('inf') for i in range(node)]
    used = [False for i in range(node)]
    d[start] = 0
    while True:
        flag = True
        for i in range(node):
            if (not used[i]) and flag:
                v = i
                flag = False
            elif (not used[i]) and d[i] < d[v]:
                v = i
        
        if flag:
            break
        used[v] = True
        for j in range(node):
            d[j] = min(d[j],d[v]+cost[v][j])
    return d

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

Solution (Python):
h,w = map(int,input().split())
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i in 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 abs(a[i][j]) != 1:
            ans += c[a[i][j]][1]

print(ans)

Solution (Python):
from collections import Counter
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])
print(sum(C[k][1]*v for k, v in Counter(a for aa in A for a in aa).items() if abs(k) != 1))

Solution (Python):
h,w = map(int,input().split())

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

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

trans_map = warshall_floyd(C)

ans = 0
for i in range(h):
    line = list(map(int,input().split()))
    for j in range(w):
        num = line[j]
        if num == -1:
            continue
        ans += trans_map[num][1]

print(ans)

Solution (Python):
from itertools import permutations 
h,w = map(int,input().split())
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i in input().split()] for _ in range(h)]
ans = 0
lst = [int(i) for i in range(10) if i != 1]
mp = [0]*10

for i in lst:
    mp[i] = c[i][1]
    for j in range(1,9):
        for k in permutations(lst,j):
            newlst = list(k)
            cnt = c[i][newlst[0]]
            for l in range(j-1):
                cnt += c[newlst[l]][newlst[l+1]]
            cnt += c[newlst[-1]][1]
            if cnt < mp[i]:
                mp[i] = cnt

for i in range(h):
    for j in range(w):
        if abs(a[i][j]) != 1:
            ans += mp[a[i][j]]

print(ans)

Solution (Python):
h,w = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(10)]
li = [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 lis[i][j] > lis[i][k] + lis[k][j]:
                lis[i][j] = lis[i][k] + lis[k][j]
ans = 0
for i in range(h):
    for num in li[i]:
        if num >= 0:
            ans += lis[num][1]
print(ans)

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
m > n impossible
m = n, possible only when n = 1


"""

H = gi()
W = gi()
cs = [[0 for x in range(10)] for i in range(10)]

for i in range(10):
    for j in range(10):
        cs[i][j] = gi()

for mid in range(10):
    for u in range(10):
        for v in range(10):
            cs[u][v] = min(cs[u][v], cs[u][mid] + cs[mid][v])
            cs[v][u] = min(cs[v][u], cs[v][mid] + cs[mid][u])

ans = 0
for i in range(H):
    for j in range(W):
        a = gi()
        if a >= 0:
            ans += cs[a][1]
print(ans)

Solution (Python):
h, w = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
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]
ans = 0
for i in range(h):
    a = list(map(int, input().split()))
    for j in range(w):
        if a[j] >= 0:
            ans += c[a[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]>=0:
            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 row in A:
    for a in row:
        if a < 0:
            continue
        ans += c[a][1]
print(ans)

Solution (Python):
from scipy.sparse.csgraph import csgraph_from_dense,floyd_warshall
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)]
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:
      ans+=d[a[i][j]][1]
print(int(ans))

Solution (Python):
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
import numpy as np
H,W=map(int,input().split(' '))
c = np.array([list(map(int,input().split(' '))) for i in range(10)])
A = [list(map(int,input().split(' '))) for i in range(H)]
csr = csr_matrix(c)
cost  = dijkstra(csr)
ans = 0
for i in A:
    for j in i:
        if j != -1:
            ans += cost[j][1]
print(int(ans))

Solution (Python):
import sys
input = sys.stdin.readline
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)]
def warshall_floyd(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
n=10
B=warshall_floyd(c)

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

Solution (Python):
h, w = map(int, input().split())

WF = []
wall = []
ans = 0

for _ in range(10):
    array = list(map(int, input().split()))
    WF.append(array)

for _ in range(h):
    array = list(map(int,input().split()))
    wall.append(array)

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

for i in range(h):
    for j in range(w):
        num = wall[i][j]
        if num != -1:
            ans += WF[num][1]

print(ans)

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

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

def f_warshall(C):
    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])

f_warshall(C)

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

print(int(cost))

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

def f_warshall(C):
  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])

f_warshall(C)

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

print(int(cost))

Solution (Python):

from scipy.sparse.csgraph import floyd_warshall

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

C = floyd_warshall(C)  

cost = 0
for i in range(H):
    for a in A[i]:
        if a != -1:
            cost += C[a][1]

print(int(cost))

Solution (Python):

from scipy.sparse.csgraph import floyd_warshall
from itertools import permutations

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

C = floyd_warshall(C)  

cost = 0
for i in range(H):
    for a in A[i]:
        if a != -1:
            cost += C[a][1]

print(int(cost))

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
from itertools import permutations

H, W = map(int, input().split())
C = [[0]*10 for _ in range(10)]
A = [[0]*W for _ in range(H)]

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

C = floyd_warshall(C)  

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

print(int(cost))

Solution (Python):
INFTY = 10**4
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)]

d = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        d[i][j] = C[i][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])

cnt = 0
for i in range(H):
    for j in range(W):
        if A[i][j]>-1:
            cnt += d[A[i][j]][1]
print(cnt)

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 n in row:
        if n == -1:
            continue
        else:
            ans += C[n][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)]



dp=[[1000*10 for _ in range(10)] for __ in range(10)]
for i in range(10):
    for j in range(10):
        dp[i][j]=C[i][j]
for i in range(10): 
    for j in range(10): 
        for k in range(10): 
            dp[j][k]=min(dp[j][k],dp[j][i]+dp[i][k])
min_mp=[0]*10
for i in range(10):
    min_mp[i]=dp[i][1]

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

Solution (Python):


from collections import Counter
from functools import reduce

def warshall(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]

h,w = map(int,input().split())
magic = [list(map(int,input().split())) for _ in range(10)]
counter = Counter()
[counter.update(Counter(map(int, input().split()))) for _ in range(h)]
warshall(magic, 10)
print(reduce(lambda x,y:x+y, [magic[k][1] * v for k,v in counter.items() if k > -1]))

Solution (Python):


from collections import Counter
from functools import reduce

def warshall(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]

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

counter = Counter()
for _ in range(h):
    counter.update(Counter(map(int, input().split())))
warshall(magic, 10)
print(reduce(lambda x,y:x+y, [magic[k][1] * v for k,v in counter.items() if k > -1]))

Solution (Python):


from collections import Counter
from functools import reduce

def warshall(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]

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

counter = {i:0 for i in range(10)}
for _ in range(h):
    for a in map(int, input().split()):
        if a > -1:
            counter[a] += 1
    
warshall(magic, 10)

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

Solution (Python):
def main():
    h,w = map(int,input().split())
    c = []
    for i in range(0,10):
        cc = list(map(int,input().split()))
        c.append(cc)
    a = []
    for i in range(0,h):
        aa = list(map(int,input().split()))
        a.append(aa)
    
    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])
    cnt = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] != 1 and a[i][j] >= 0:
                cnt += c[a[i][j]][1]
    print(cnt)


if __name__=='__main__':
    main()

Solution (Python):
def main():
    h,w = map(int,input().split())
    c = []
    for i in range(0,10):
        cc = list(map(int,input().split()))
        c.append(cc)
    a = []
    for i in range(0,h):
        aa = list(map(int,input().split()))
        a.append(aa)
    
    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])
    cnt = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] == -1:
                continue
            else:
                cnt += c[a[i][j]][1]
    print(cnt)


if __name__=='__main__':
    main()

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 += 0
        else:
            ans += C[A[i][j]][1]
print(ans)

Solution (Python):
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph._shortest_path import floyd_warshall


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


if __name__ == '__main__':
    solve()

Solution (Python):

h, w = map(int, input().split())
INF = 10 ** 10
table = [[INF] * 10 for _ in range(10)]

for i in range(10):
    column = list(map(int, input().split()))
    for j in range(10):
        if i == j:
            continue
        table[i][j] = column[j]

wall = {}
for _ in range(h):
    for a in list(map(int, input().split())):
        if a not in wall:
            wall[a] = 1
        else:
            wall[a] += 1

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

ans = 0
for k, v in wall.items():
    if k != -1 and k != 1:
        ans += table[k][1] * v
print(ans)

Solution (Python):



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

wall = {}
for _ in range(h):
    x = list(map(int, input().split()))
    for v in x:
        if v not in wall:
            wall[v] = 1
        else:
            wall[v] += 1


def rec(idx, cost, used):
    if idx == 1:
        return cost
    ret = 10 ** 10
    for i in range(10):
        if i in used:
            continue
        next_used = used[:]
        next_used.append(i)
        ret = min(ret, rec(i, cost + table[idx][i], next_used))
    return ret


arr = [0 for _ in range(10)]
for i in range(10):
    if i == 1:
        continue
    arr[i] = rec(i, 0, [i])

ans = 0
for k, v in wall.items():
    if k != -1 and k != 1:
        ans += arr[k] * v
print(ans)

Solution (Python):
import itertools

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


to1 = [10**3 for i in range(10)]

nlist = [0] + [i for i in range(2,10)]

for i in range(1,10):
    for v in itertools.permutations(nlist,i):
        c = 0
        for j in range(i-1):
            c += cost[v[j]][v[j+1]]
        c += cost[v[i-1]][1]
        to1[v[0]] = min(to1[v[0]], c)

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

print(ans)

Solution (Python):
H,W = map(int,input().split())
C = [list(map(int,input().split())) for _ in range(10)]
cntA = {}
for _ in range(H):
  for a in map(int,input().split()):
    if a not in cntA:
      cntA[a] = 1
    else:
      cntA[a] += 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])
      
ans = 0
for key, num in cntA.items():
  if key not in [-1,1]:
    ans += C[key][1]*num
print(ans)