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-----
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-----
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 8s, so the minimum total MP required is 6×2=12.

Difficulty: MEDIUM_HARD

Solutions:
(H, W) = list(map(int, input().split()))
C = []
for i in range(10):
	tmp = list(map(int, input().split()))
	C.append(tmp)
A = []
for i in range(H):
	tmp = list(map(int, input().split()))
	A.append(tmp)
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 h in range(H):
	for w in range(W):
		if A[h][w] == -1 or A[h][w] == 1:
			continue
		ans += C[A[h][w]][1]
print(ans)


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


(n, m) = map(int, input().split())
dist = [list(map(int, input().split())) for i in range(10)]
dist.append([0] * 10)
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])
ans = 0
for i in range(n):
	a = list(map(int, input().split()))
	for j in a:
		ans += dist[j][1]
print(ans)


(h, w) = map(int, input().split())
c = []
for i 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):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for i in range(h):
	s = list(map(int, input().split()))
	for j in s:
		if j != 1 and j != -1:
			ans += c[j][1]
print(ans)


(h, w) = map(int, input().split())
g = []
for _ in range(10):
	a = list(map(int, input().split()))
	g.append(a)
for k in range(10):
	for i in range(10):
		for j in range(10):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
ans = 0
for i in range(h):
	ans += sum(map(lambda x: g[x][1], filter(lambda x: x != -1, map(int, input().split()))))
print(ans)


(H, W) = list(map(int, input().split()))
cost = [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):
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
ans = 0
for i in range(H):
	for j in range(W):
		now = A[i][j]
		if now != -1:
			ans += cost[now][1]
print(ans)


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


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


from collections import Counter
from functools import reduce
(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):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])
A = Counter(map(int, reduce(lambda a, b: a + b, [input().split() for _ in range(H)])))
print(sum([c[i][1] * A[i] for i in range(10) if i in A]))


import sys
readline = sys.stdin.readline
(H, W) = map(int, readline().split())
C = [None] * 10
for i in range(10):
	C[i] = list(map(int, readline().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])
from collections import defaultdict
wall = defaultdict(int)
for i in range(H):
	for w in list(map(int, readline().split())):
		wall[w] += 1
ans = 0
for (key, value) in wall.items():
	if key == -1:
		continue
	ans += value * C[key][1]
print(ans)


(h, w) = map(int, input().split())
c = [list(map(int, input().split())) for _ in [0] * 10]
a = [list(map(int, input().split())) for _ in [0] * h]
cost = [float('inf')] * 10
cost[1] = 0
for _ in [0] * 9:
	for i in range(10):
		cost[i] = min((c[i][j] + cost[j] for j in range(10)))
ans = 0
for i in range(h):
	for j in range(w):
		if a[i][j] >= 0:
			ans += cost[a[i][j]]
print(ans)


(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):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for _ in range(H):
	ans += sum([c[x][1] for x in map(int, input().split()) if x != -1])
print(ans)


import copy
(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 = copy.deepcopy(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 al in A:
	for a in al:
		if a != -1:
			ans += d[a][1]
print(ans)


(h, w) = map(int, input().split())
d = [list(map(int, input().split())) for i in range(10)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			d[i][j] = min(d[i][k] + d[k][j], d[i][j])
ans = 0
for i in range(h):
	v = map(int, input().split())
	for x in v:
		if x >= 0:
			ans += d[x][1]
print(ans)


(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):
			if c[i][k] + c[k][j] < c[i][j]:
				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:
			ans += c[A[h][w]][1]
print(ans)


(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for i 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]
s = 0
for i in range(H):
	a = list(map(int, input().split()))
	for j in range(W):
		if a[j] == -1:
			a[j] = 1
		s += c[a[j]][1]
print(s)


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


from collections import Counter
(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for i in range(10)]
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])
count = Counter()
for i in range(H):
	count.update(list(map(int, input().split())))
print(sum([c[i][1] * count[i] for i in range(10)]))


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


(H, W) = map(int, input().split())
mp = [list(map(int, input().split())) for _ in range(10)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j])
rlt = 0
for i in range(H):
	for j in map(int, input().split()):
		if j != -1 and j != 1:
			rlt += mp[j][1]
print(rlt)


from copy import copy
(h, w) = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(10)]
c = [list(map(int, input().split())) for _ in range(h)]
d = copy(data)

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


def warshall_floyd():
	for middle in range(10):
		for start in range(10):
			for end in range(10):
				costs[start][end] = min(costs[start][end], costs[start][middle] + costs[middle][end])
(H, W) = map(int, input().split())
costs = []
for i in range(10):
	costs.append(list(map(int, input().split())))
warshall_floyd()
ans = 0
for i in range(H):
	wall = list(map(int, input().split()))
	for num in wall:
		if num != -1:
			ans += costs[num][1]
print(ans)


import heapq
(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)]
flag = [[1 for _ in range(10)] for _ in range(10)]
for i in range(10):
	que = [[0, i]]
	while que:
		now = heapq.heappop(que)
		flag[i][now[1]] = 0
		for j in range(10):
			if flag[i][j]:
				heapq.heappush(que, [c[i][now[1]] + c[now[1]][j], j])
				if c[i][now[1]] + c[now[1]][j] < c[i][j]:
					c[i][j] = c[i][now[1]] + c[now[1]][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)


import sys
sys.setrecursionlimit(10 ** 9)

def int1(x):
	return int(x) - 1

def II():
	return int(input())

def MI():
	return list(map(int, input().split()))

def MI1():
	return list(map(int1, input().split()))

def LI():
	return list(map(int, input().split()))

def LI1():
	return list(map(int1, input().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def MS():
	return input().split()

def LS():
	return list(input())

def LLS(rows_number):
	return [LS() for _ in range(rows_number)]

def printlist(lst, k=' '):
	print(k.join(list(map(str, lst))))
INF = float('inf')
import numpy as np

def solve():
	(H, W) = MI()
	dist = LLI(10)
	N = 10
	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])
	ans = 0
	for i in range(H):
		for r in LI():
			if r != -1:
				ans += dist[r][1]
	print(ans)

def __starting_point():
	solve()
__starting_point()


(h, w) = list(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(adj_m):
	_dist = adj_m
	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
dist = warshall_floyd(c)
ans = 0
for i in range(h):
	for j in range(w):
		if a[i][j] != -1:
			ans += dist[a[i][j]][1]
print(ans)


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


(h, w) = map(int, input().split())
c = []
for _ in range(10):
	line = list(map(int, input().split()))
	c.append(line)
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])
res = 0
for _ in range(h):
	for a in map(int, input().split()):
		if a >= 0:
			res += c[a][1]
print(res)


(h, w) = map(int, input().split())
d = []
for i in range(10):
	d.append(list(map(int, input().split())))
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])
rslt = 0
for i in range(h):
	for j in map(int, input().split()):
		if j != -1:
			rslt += d[j][1]
print(rslt)


(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())))
cost = [float('inf') for i in range(10)]
flag = [False for i in range(10)]
cost[1] = 0
test = [1]
while test:
	num = test.pop(0)
	flag[num] = True
	for i in range(10):
		cost[i] = min(cost[i], cost[num] + c[i][num])
	temp = [[cost[i], i] for i in range(10)]
	temp.sort()
	for i in range(10):
		if flag[temp[i][1]] == True:
			continue
		else:
			test.append(temp[i][1])
			break
ans = 0
for i in range(h):
	for j in range(w):
		if a[i][j] == -1:
			continue
		else:
			ans += cost[a[i][j]]
print(ans)


def warshall_floyd():
	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])
(H, W) = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
warshall_floyd()
res = 0
for _ in range(H):
	A = list(map(int, input().split()))
	res += sum((C[a][1] for a in A if a != -1))
print(res)


import sys
readline = sys.stdin.readline
(H, W) = map(int, readline().split())
C = [None] * 10
for i in range(10):
	C[i] = list(map(int, readline().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):
	line = list(map(int, readline().split()))
	for j in range(len(line)):
		if line[j] == -1:
			continue
		ans += C[line[j]][1]
print(ans)


from itertools import *
(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for i in range(10 + H)]
for (k, i, j) in product(list(range(10)), repeat=3):
	c[i][j] = min(c[i][j], c[i][k] + c[k][j])
print(sum((c[c[i + 10][j]][1] if c[i + 10][j] != -1 else 0 for j in range(W) for i in range(H))))


(h, w) = map(int, input().split())
cost = [list(map(int, input().split())) for i in range(10)]
wall = [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):
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
tot_cost = 0
for i in range(h):
	for j in range(w):
		num = wall[i][j]
		if num != -1:
			tot_cost += cost[num][1]
print(tot_cost)


(h, w) = map(int, input().split())
ma = [list(map(int, input().split())) for _ in range(10)]
for i in range(10):
	for j in range(10):
		for k in range(10):
			ma[j][k] = min(ma[j][k], ma[j][i] + ma[i][k])
li = [list(map(int, input().split())) for _ in range(h)]
ans = 0
for each_li in li:
	for l in each_li:
		if l != -1:
			ans += ma[l][1]
print(ans)


from collections import defaultdict
(H, W) = list(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 list(count.items()):
	if k == -1 or k == 1:
		continue
	ans += v * dists[k][1]
print(ans)


(H, W) = map(int, input().split())
C = []
wall = []
for _ in range(10):
	C.append(list(map(int, input().split())))
for _ in range(H):
	wall += 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 x in wall:
	if x == -1:
		continue
	else:
		ans += C[x][1]
print(ans)


(H, W) = map(int, input().split())
ds = [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):
			ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j])
ans = 0
for row in A:
	for a in row:
		if a < 0:
			continue
		ans += ds[a][1]
print(ans)


(H, W) = map(int, input().split())
S = [list(map(int, input().split())) for i in range(10)]
l = [None] * 10
for i in range(10):
	l[i] = S[i][1]
for i in range(7):
	for j in range(10):
		for k in range(10):
			if l[j] > l[k] + S[j][k]:
				l[j] = l[k] + S[j][k]
l.append(0)
a = 0
for i in range(H):
	L = list(map(int, input().split()))
	for j in L:
		a += l[j]
print(a)


(h, w) = map(int, input().split())
rg = [list(map(int, input().split())) for _ in range(10)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			rg[i][j] = min(rg[i][j], rg[i][k] + rg[k][j])
atable = [list(map(int, input().split())) for _ in range(h)]
g = 0
for i in range(h):
	for j in range(w):
		if atable[i][j] != -1:
			g += rg[atable[i][j]][1]
print(g)


(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)]
for i in range(10):
	for j in range(10):
		for k in range(10):
			l[j][k] = min(l[j][k], l[j][i] + l[i][k])
ansL = [l[i][1] for i in range(10)]
ct = 0
for i in range(h):
	for j in range(w):
		if a[i][j] >= 0:
			ct += ansL[a[i][j]]
print(ct)


(h, w) = map(int, input().split())
cc = [[int(x) for x in input().split()] for i in range(10)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			cc[i][j] = min(cc[i][j], cc[i][k] + cc[k][j])
ans = 0
for i in range(h):
	aa = map(int, input().split())
	for a in aa:
		if a >= 0:
			ans += cc[a][1]
print(ans)


(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])
ans = 0
for _ in range(h):
	arr = list(map(int, input().split()))
	for i in arr:
		if abs(i) != 1:
			ans += d[i][1]
print(ans)


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)


import sys
input = sys.stdin.readline

def __starting_point():
	(H, W) = map(int, input().split())
	V = 10
	edges = []
	for i in range(10):
		c = list(map(int, input().split()))
		edges.append([(j, c[j]) for j in range(10)])
	INF = float('inf')
	dp = [[INF] * V for _ in range(V)]
	for i in range(V):
		for (j, w) in edges[i]:
			dp[i][j] = w
	for k in range(V):
		for i in range(V):
			for j in range(V):
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
	ans = 0
	for _ in range(H):
		for x in map(int, input().split()):
			if x >= 0:
				ans += dp[x][1]
	print(ans)
__starting_point()


def main():
	(H, W) = [int(x) for x in input().split(' ')]
	C = []
	for i in range(10):
		c = [int(m) for m in input().split(' ')]
		C.append(c)
	A = [0] * 10
	for i in range(H):
		a = [int(m) for m in input().split(' ')]
		for j in range(W):
			if a[j] != -1:
				A[a[j]] += 1
	MP = [[C[i][1] for i in range(10)]]
	for i in range(9):
		MP.append([min([C[k][j] + MP[-1][j] for j in range(10)]) for k in range(10)])
	minMP = [10000] * 10
	for i in range(len(MP)):
		for j in range(len(MP[i])):
			minMP[j] = min([minMP[j], MP[i][j]])
	print(sum([p * q for (p, q) in zip(A, minMP)]))
main()


(h, w) = map(int, input().split())
c = [list(map(int, input().split())) for l in range(10)]
a = []
res = 0
for _ in range(h):
	a.extend(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]
for sa in a:
	if sa != -1:
		res += c[sa][1]
print(res)


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)


def main():
	from sys import stdin

	def input():
		return stdin.readline().strip()
	(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):
				if c[i][j] > c[i][k] + c[k][j]:
					c[i][j] = c[i][k] + c[k][j]
	ans = 0
	for i in a:
		for j in i:
			if j == -1:
				continue
			else:
				ans += c[j][1]
	print(ans)
main()


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


v = 10
(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(v)]
A = [list(map(int, input().split())) for _ in range(H)]
for k in range(v):
	for i in range(v):
		for j in range(v):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for Ai in A:
	for a in Ai:
		if a == -1:
			continue
		ans += c[a][1]
print(ans)


INF = 10 ** 18
n = 10
(h, w) = map(int, input().split())
dist = [list(map(int, input().split())) for _ in range(n)]
a = [list(map(int, input().split())) for _ in range(h)]
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])
ans = 0
for i in range(h):
	for j in range(w):
		if a[i][j] == -1:
			continue
		ans += dist[a[i][j]][1]
print(ans)


[H, W] = [int(s) for s in input().rstrip().split(' ')]
L = []
for num in range(10):
	L.append([int(s) for s in input().rstrip().split(' ')])
for k in range(10):
	for i in range(10):
		for j in range(10):
			L[i][j] = min(L[i][j], L[i][k] + L[k][j])
k = 0
for h in range(H):
	L_h = [int(s) for s in input().rstrip().split(' ')]
	for x in L_h:
		if x != -1:
			k += L[x][1]
print(k)


from collections import defaultdict
from heapq import heappop, heappush

class Graph(object):

	def __init__(self):
		self.graph = defaultdict(list)

	def __len__(self):
		return len(self.graph)

	def add_edge(self, src, dst, weight=1):
		self.graph[src].append((dst, weight))

	def get_nodes(self):
		return self.graph.keys()

class Dijkstra(object):

	def __init__(self, graph, start):
		self.g = graph.graph
		self.dist = defaultdict(lambda : float('inf'))
		self.dist[start] = 0
		self.prev = defaultdict(lambda : None)
		self.Q = []
		heappush(self.Q, (self.dist[start], start))
		while self.Q:
			(dist_u, u) = heappop(self.Q)
			if self.dist[u] < dist_u:
				continue
			for (v, weight) in self.g[u]:
				alt = dist_u + weight
				if self.dist[v] > alt:
					self.dist[v] = alt
					self.prev[v] = u
					heappush(self.Q, (alt, v))

	def shortest_distance(self, goal):
		return self.dist[goal]

	def shortest_path(self, goal):
		path = []
		node = goal
		while node is not None:
			path.append(node)
			node = self.prev[node]
		return path[::-1]
(h, w) = map(int, input().split())
g = Graph()
for i in range(10):
	c = list(map(int, input().split()))
	for j in range(10):
		(src, dst, weight) = (i, j, c[j])
		g.add_edge(src, dst, weight)
ans = 0
for y in range(h):
	tmp = list(map(int, input().split()))
	for x in range(w):
		if tmp[x] != -1 and tmp[x] != 1:
			d = Dijkstra(g, tmp[x])
			ans += d.shortest_distance(1)
print(ans)


(h, w) = list(map(int, input().split()))
n = 10
dist = [list(map(int, input().split())) for _ in range(n)]
wall = [list(map(int, input().split())) for _ in range(h)]
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])
ans = 0
for i in range(h):
	for j in range(w):
		if wall[i][j] != -1:
			ans += dist[wall[i][j]][1]
print(ans)


import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal

def s():
	return input()

def i():
	return int(input())

def S():
	return input().split()

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

def L():
	return list(input().split())

def l():
	return list(map(int, input().split()))

def lcm(a, b):
	return a * b // math.gcd(a, b)
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 9
mod = 10 ** 9 + 7
(H, W) = I()
cost = [l() for _ in range(10)]
A = [l() for _ in range(H)]
ans = 0
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])
for i in range(H):
	for j in range(W):
		if A[i][j] > -1:
			ans += cost[A[i][j]][1]
print(ans)


(h, w) = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(10)]
o = [list(map(int, input().split())) for _ in range(h)]
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]
v = [[l[j][1] if j >= 0 else 0 for j in i] for i in o]
print(sum(map(sum, v)))


(H, W) = map(int, input().split())
(c, A, s) = ([], [], 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 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])
for i in range(10):
	for j in range(H):
		s += A[j].count(i) * c[i][1]
print(s)


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


N = 10
(h, w) = map(int, input().split())
c = [list(map(int, input().split())) for i in range(N)]
a = [list(map(int, input().split())) for i in range(h)]
for k in range(N):
	for i in range(N):
		for j in range(N):
			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] >= 0:
			s += c[a[i][j]][1]
print(s)


(H, W) = map(int, input().split())
C = []
A = []
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 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])
print(sum((sum((C[i][1] for i in AA if i >= 0)) for AA in A)))


(h, w) = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
dp = [0] * 10
for i in range(10):
	dp[i] += c[i][1]
for _ in range(11):
	for j in range(10):
		for k in range(10):
			dp[j] = min(dp[j], dp[k] + c[j][k])
count = 0
for _ in range(h):
	a = list(map(int, input().split()))
	for l in a:
		if l != -1:
			count += dp[l]
print(count)


(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([sum(map(lambda a: 0 if a == -1 else C[a][1], row)) for row in A]))


(H, W) = map(int, input().split())
INF = 10 ** 4
c = [[int(i) for i in input().split()] for _ in range(10)]
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 _ in range(H):
	for a in map(int, input().split()):
		if a != -1:
			ans += c[a][1]
print(ans)


from itertools import permutations
from heapq import heappush, heappop
INF = 10 ** 12

def dijkstra(G, start=0, INF=INF):
	d = [INF for i in range(len(G))]
	d[start] = 0
	que = []
	heappush(que, (0, start))
	while len(que) != 0:
		p = heappop(que)
		v = p[1]
		if d[v] < p[0]:
			continue
		for u in G[v].keys():
			if d[u] > d[v] + G[v][u]:
				d[u] = d[v] + G[v][u]
				heappush(que, (d[u], u))
	return d
(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)]
G = [{} for _ in range(10)]
for i in range(10):
	for j in range(10):
		G[i][j] = c[i][j]
dist = [0] * 10
for i in range(10):
	d = dijkstra(G, i)
	dist[i] = d[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)


(h, w) = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(10)]
for i in range(10):
	for j in range(10):
		for k in range(10):
			graph[j][k] = min(graph[j][k], graph[j][i] + graph[i][k])
ans = 0
for l in range(h):
	tile = list(map(int, input().split()))
	for m in tile:
		if m != -1:
			ans += graph[m][1]
print(ans)


import sys
read = sys.stdin.read
from collections import Counter

def main():

	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
	(h, w) = list(map(int, input().split()))
	n = 10
	d = []
	for _ in range(10):
		d.append(list(map(int, input().split())))
	d = warshall_floyd(d)
	a = []
	for _ in range(h):
		row = list(map(int, input().split()))
		row = [1 if i == -1 else i for i in row]
		a.extend(row)
	aa = Counter(a)
	ans = 0
	for i0 in range(10):
		ans += aa[i0] * d[i0][1]
	print(ans)

def __starting_point():
	main()
__starting_point()


(h, _) = map(int, input().split())
r = range(10)
c = [[int(i) for i in input().split()] for _ in r]
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])
else:
	a = [[int(i) for i in input().split()] for _ in range(h)]
	print(sum((c[i][1] for i in sum(a, []) if i != -1)))


(H, W) = map(int, input().split())
C = [list(map(int, input().split())) for i in range(10)]
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):
	A = list(map(int, input().split()))
	for a in A:
		if a >= 0:
			ans += C[a][1]
print(ans)


import heapq
(H, W) = map(int, input().split())
C = []
for i in range(10):
	C.append(list(map(int, input().split())))
INF = float('inf')

def dfs(x):
	memo = [INF] * 10
	memo[x] = 0
	hq = [(0, x)]
	heapq.heapify(hq)
	while hq:
		(cost, step) = heapq.heappop(hq)
		if cost > memo[step]:
			continue
		for (j, c) in enumerate(C[step]):
			if memo[j] > memo[step] + c:
				memo[j] = memo[step] + c
				heapq.heappush(hq, (memo[j], j))
	return memo[1]
A = [0] * 10
for h in range(H):
	for w in map(int, input().split()):
		if w == -1:
			continue
		else:
			A[w] += 1
ans = 0
for (number, a) in enumerate(A):
	ans += a * dfs(number)
print(ans)


(H, W) = map(int, input().split())
dp = [list(map(int, input().split())) for _ in range(10)]
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])
ans = 0
for i in range(H):
	line = list(map(int, input().split()))
	for j in line:
		if j == -1:
			continue
		ans += dp[j][1]
print(ans)


import sys

def warshall_floyd():
	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])
(H, W) = list(map(int, input().split()))
inf = float('inf')
C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]
A = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(H)]
warshall_floyd()
res = 0
for row in A:
	for col in row:
		if col != -1:
			res += C[col][1]
print(res)


(H, W) = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(10)]
for i in range(10):
	for j in range(10):
		for k in range(10):
			s[j][k] = min(s[j][k], s[j][i] + s[i][k])
ans = 0
change = [s[i][1] for i in range(10)]
for _ in range(H):
	t = list(map(int, input().split()))
	ans += sum((change[i] for i in t if i != -1))
print(ans)


(H, W) = map(int, input().split())
X = [[int(a) for a in input().split()] for _ in range(10)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			X[i][j] = min(X[i][j], X[i][k] + X[k][j])
print(sum([sum([0 if a == '-1' else X[int(a)][1] for a in input().split()]) for _ in range(H)]))


import sys
input = sys.stdin.readline

def read():
	(H, W) = list(map(int, input().strip().split()))
	N = 10
	C = []
	for i in range(N):
		c = list(map(int, input().strip().split()))
		C.append(c)
	A = []
	for i in range(H):
		a = list(map(int, input().strip().split()))
		A.append(a)
	return (H, W, N, C, A)

def warshall_floyd(N, G, INF=10 ** 7):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				G[i][j] = min(G[i][j], G[i][k] + G[k][j])
	return G

def solve(H, W, N, C, A):
	D = warshall_floyd(N, C)
	ans = 0
	for i in range(H):
		for j in range(W):
			a = A[i][j]
			if a >= 0:
				ans += D[a][1]
	return ans

def __starting_point():
	inputs = read()
	outputs = solve(*inputs)
	if outputs is not None:
		print('%s' % str(outputs))
__starting_point()


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

def f(n):
	for i in range(10):
		flag = False
		cc = C[i][n] + cost[n]
		if cc < cost[i]:
			flag = True
			cost[i] = cc
			f(i)
f(1)
ans = 0
for _ in range(H):
	for n in map(int, input().split()):
		if n < 0:
			continue
		ans += cost[n]
print(ans)


(h, w) = map(int, input().split())
c = [[*map(int, input().split())] for _ in range(10)]
a = [[*map(int, input().split())] for _ in range(h)]
d = [c[i][1] for i in range(10)]
for i in range(10):
	for j in range(10):
		d[j] = min(d[j], min((c[j][k] + d[k] for k in range(10))))
d += [0]
print(sum((d[i] for i in sum(a, []))))


(h, w) = map(int, input().split())
INF = float('inf')
a = []
for _ in range(10):
	c = list(map(int, input().split()))
	a.append(c)
for i in range(10):
	for j in range(10):
		for k in range(10):
			a[j][k] = min(a[j][k], a[j][i] + a[i][k])
ans = 0
for x in range(h):
	d = list(map(int, input().split()))
	for e in d:
		if e != -1:
			ans += a[e][1]
print(ans)


(H, W) = [int(_) for _ in input().split()]
C = [[int(_) for _ in input().split()] for _ in range(10)]
A = [[int(_) for _ in input().split()] for _ in range(H)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			C[i][1] = min(C[i][1], C[i][j] + C[j][1])
ans = 0
for h in range(H):
	for w in range(W):
		if A[h][w] != -1:
			v = A[h][w]
			ans += C[v][1]
print(ans)


N = 10
MAX_C = 1000
(H, W) = [int(x) for x in input().split()]
C = [[int(x) for x in input().split()] for _ in range(N)]
dist = [0 if i == 1 else MAX_C for i in range(N)]

def bellman_ford():
	for _ in range(N - 1):
		for u in range(N):
			for v in range(N):
				dist[v] = min(dist[v], dist[u] + C[v][u])
bellman_ford()
ans = sum([sum([0 if x == '-1' else dist[int(x)] for x in input().split()]) for _ in range(H)])
print(ans)


(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])
spell = 0
for wist in a:
	for val in wist:
		if val == -1:
			continue
		spell += c[val][1]
print(spell)


(H, W) = map(int, input().split())
Cls = []
for i in range(10):
	Cls.append(list(map(int, input().split())))
for k in range(10):
	for i in range(10):
		for j in range(10):
			Cls[i][j] = min(Cls[i][j], Cls[i][k] + Cls[k][j])
costls = []
for i in range(10):
	costls.append(Cls[i][1])
numls = []
for i in range(H):
	numls += list(map(int, input().split()))
ans = 0
for i in range(H * W):
	if numls[i] == -1:
		continue
	ans += costls[numls[i]]
print(ans)


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


(h, w) = map(int, input().split())
cost = [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):
			cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k])
ans = 0
for i in range(h):
	a = map(int, input().split())
	for j in a:
		if j == -1:
			continue
		ans += cost[j][1]
print(ans)


(H, W) = map(int, input().split())
d = []
for i in range(10):
	l = list(map(int, input().split()))
	d.append(l)
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 i in range(H):
	l = list(map(int, input().split()))
	for j in l:
		if j == -1:
			continue
		ans += d[j][1]
print(ans)


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


(H, W) = map(int, input().split())
dist = [list(map(int, input().split())) for _ in range(10)]
for i in range(10):
	for j in range(10):
		for k in range(10):
			dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k])
ans = 0
for _ in range(H):
	for i in list(map(int, input().split())):
		if i == 1 or i == -1:
			continue
		ans += dist[i][1]
print(ans)


(h, w) = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
num = [0] * 10
for _ in range(h):
	a = list(map(int, input().split()))
	for i in a:
		if i == -1:
			continue
		num[i] += 1
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])
print(sum((num[x] * c[x][1] for x in range(10))))


(H, W) = map(int, input().split())
c = []
for i 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):
			c[i][j] = min(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 A:
		if j >= 0:
			ans += c[j][1]
print(ans)


from collections import defaultdict
dicA = defaultdict(int)
(H, W) = map(int, input().split())
C = []
min_step = []
for i in range(10):
	temp = list(map(int, input().split()))
	C.append(temp)
	min_step.append(temp[1])

def dfs(n, k, now):
	if now > min_step[n]:
		return
	for i in range(10):
		if i == 1:
			min_step[n] = min(min_step[n], now + C[k][1])
		if i != k:
			if C[k][i] + now < min_step[n]:
				dfs(n, i, C[k][i] + now)
for i in range(10):
	if i == 1:
		continue
	for j in range(10):
		if i != j:
			dfs(i, j, C[i][j])
for i in range(H):
	temp = list(map(int, input().split()))
	for j in range(W):
		if temp[j] == -1:
			continue
		dicA[temp[j]] += 1
ans = 0
for x in dicA.keys():
	ans += min_step[x] * dicA[x]
print(ans)


def main():
	(H, W) = list(map(int, input().split()))
	max_num = 10
	c = [list(map(int, input().split())) for _ in range(max_num)]
	A = [list(map(int, input().split())) for _ in range(H)]
	for k in range(max_num):
		for i in range(max_num):
			for j in range(max_num):
				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)

def __starting_point():
	main()
__starting_point()


(H, W) = list(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 main():
	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[a][1] for column in A for a in column if a >= 0)))

def __starting_point():
	main()
__starting_point()


INF = 2 * 10 ** 7 * 100 + 1234567
(H, W) = map(int, input().split())
c = [[int(e) for e in input().split()] for i 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] and c[i][k] != INF and (c[k][j] != INF):
				c[i][j] = c[i][k] + c[k][j]
ans = 0
for i in range(H):
	A_row = [int(e) for e in input().split()]
	for A in A_row:
		if A != -1:
			ans += c[A][1]
print(ans)


(H, W) = map(int, input().split())
C = [list(map(int, input().split())) for i in range(10)]
G = [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 G:
	for x in row:
		if x >= 0:
			ans += C[x][1]
print(ans)


from collections import defaultdict
(h, w) = map(int, input().split())
c = []
dic = defaultdict(int)
for _ in range(10):
	c.append(list(map(int, input().split())))
for _ in range(h):
	temp = list(map(int, input().split()))
	for i in temp:
		dic[i] += 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 item in dic.items():
	if item[0] >= 0:
		ans += item[1] * c[item[0]][1]
print(ans)


INF = 10 ** 9
(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for i in range(10)]
dp = [[INF if i != j else 0 for j in range(10)] for i in range(10)]
for k in range(0, 10, 1):
	for i in range(0, 10, 1):
		for j in range(0, 10, 1):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])
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:
			ans += c[A[i][j]][1]
print(ans)


(H, W) = list(map(int, input().split()))
C = []
for _ in range(10):
	c = list(map(int, input().split()))
	C.append(c)
A = []
for _ in range(H):
	a = list(map(int, input().split()))
	A.append(a)
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])
count = 0
for i in range(H):
	for j in range(W):
		if A[i][j] == -1:
			continue
		else:
			count += C[A[i][j]][1]
print(count)


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
m = [c[i][1] for i in r]
for _ in r:
	for i in r:
		for j in r:
			m[i] = min(m[i], c[i][j] + m[j])
print(sum((m[i] * C[i] for i in r)))


from math import gcd
from itertools import combinations, permutations, accumulate, product
from collections import deque, defaultdict, Counter
import decimal
import re
import math
import bisect
import heapq
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
INF = float('inf')
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())

class WarshallFloyd(object):

	def __init__(self):
		self.max_v = 10
		self.min_dist = [readInts() for _ in range(10)]

	def compute(self):
		for (k, i, j) in product(list(range(self.max_v)), repeat=3):
			self.min_dist[i][j] = min(self.min_dist[i][j], self.min_dist[i][k] + self.min_dist[k][j])

	def solve(self, n):
		if n == 1:
			return 0
		else:
			return self.min_dist[n][1]
(h, w) = readInts()
w = WarshallFloyd()
w.compute()
ans = 0
A = [readInts() for _ in range(h)]
for a in A:
	for i in a:
		if i == -1:
			continue
		ans += w.solve(i)
print(ans)


(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(10)]
b = [list(map(int, input().split())) for i in range(h)]
for h in range(10):
	for i in range(10):
		for j in range(10):
			if a[i][j] > a[i][h] + a[h][j]:
				a[i][j] = a[i][h] + a[h][j]
print(sum([sum([a[d][1] if d >= 0 else 0 for d in i]) for i in b]))


(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 x in range(10):
	for y in range(10):
		for z in range(10):
			c[y][z] = min(c[y][z], c[y][x] + c[x][z])
ans = 0
for x in range(h):
	for y in range(w):
		if a[x][y] >= 0:
			ans += c[a[x][y]][1]
print(ans)


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


(H, W) = map(int, input().split())
D = []
for i in range(10):
	Cs = list(map(int, input().split()))
	D.append(Cs)
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])
r = 0
for i in range(H):
	As = list(map(int, input().split()))
	for j in range(W):
		A = As[j]
		if A < 0:
			continue
		r += D[A][1]
print(r)


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

def w_f(C):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				C[i][j] = min(C[i][j], C[i][k] + C[k][j])
	return C
C = w_f(C)
A = []
for i in range(H):
	a = list(map(int, input().split()))
	A.append(a)
ans = 0
for h in range(H):
	for w in range(W):
		if A[h][w] != -1:
			ans += C[A[h][w]][1]
print(ans)


(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
A = [n for _ in range(H) for n in list(map(int, input().split()))]
o = [c[n][1] for n in range(10)]
for _ in range(10):
	for i in range(10):
		for t in range(10):
			o[i] = min(o[i], c[i][t] + o[t])
print(sum([o[a] for a in A if a >= 0]))


(H, W) = map(int, input().split())
C = []
for i in range(10):
	c = list(map(int, input().split()))
	C.append(c)
A = []
for i in range(H):
	a = list(map(int, input().split()))
	A.append(a)
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]
ans = 0
for i in range(H):
	l = A[i]
	for j in range(W):
		n = l[j]
		if n == 0 or n > 1:
			ans += C[n][1]
print(ans)


(h, w) = map(int, input().split())
c = [list(map(int, input().split())) for i in range(10)]
for v in range(10):
	for s in range(10):
		for g in range(10):
			c[s][g] = min(c[s][g], c[s][v] + c[v][g])
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 += c[a[i][j]][1]
print(ans)


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

def main():
	(h, w) = list(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 = 0
	results = warshall_floyd(c)
	for i in range(h):
		for j in range(w):
			if a[i][j] != -1:
				ai = a[i][j]
				ans += results[ai][1]
	print(ans)

def __starting_point():
	main()
__starting_point()


def Wall():
	(h, w) = list(map(int, input().split()))
	wf = [list(map(int, input().split())) for _ in range(10)]
	for i in range(10):
		wf[i][i] = 0
	for i in range(10):
		for j in range(10):
			for k in range(10):
				wf[j][k] = min(wf[j][i] + wf[i][k], wf[j][k])
	ans = 0
	for _ in range(h):
		a = list(map(int, input().split()))
		for v in a:
			if v == -1:
				continue
			ans += wf[v][1]
	print(ans)

def __starting_point():
	Wall()
__starting_point()


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


(h, w) = map(int, input().split())
costs = [list(map(int, input().split())) for _ in range(10)]
for k in range(10):
	for i in range(10):
		for j in range(10):
			costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j])
ans = 0
for _ in range(h):
	for n in map(int, input().split()):
		if n == -1:
			continue
		ans += costs[n][1]
print(ans)


(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])
answer = 0
for i in range(H):
	for j in range(W):
		if A[i][j] == -1:
			continue
		answer += C[A[i][j]][1]
print(answer)


(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for _ in [0] * 10]
a = [list(map(int, input().split())) for _ in [0] * H]

def dp(i, j, l, lim):
	if lim < 0:
		return 10 ** 4
	else:
		return min([c[i][j]] + [c[i][k] + dp(k, j, l + [k], lim - c[i][k]) for k in range(10) if k not in l])
cc = [dp(i, 1, [i, 1], c[i][1]) for i in range(10)]
print(sum([cc[j] for i in a for j in i if j != -1]))


import copy
L = [[j for j in range(10)] for i in range(10)]
(H, W) = map(int, input().split())
costs = list()
costs2 = list()
for i in range(10):
	L1 = list(map(int, input().split()))
	costs.append(L1[1])
	costs2.append(L1[1])
	L[i] = L1
for i in range(12):
	for j in range(10):
		for k in range(10):
			costs2[j] = min(costs2[j], costs[k] + L[j][k])
	costs = copy.copy(costs2)
costs.append(0)
s = 0
for i in range(H):
	A = list(map(int, input().split()))
	s += sum([costs[i] for i in A])
print(s)


def warshall_floyd(V):
	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])
from collections import Counter
INF = float('inf')
(H, W) = list(map(int, input().split()))
d = [[int(i) for i in input().split()] for _ in range(10)]
a = []
for _ in range(H):
	a.extend([int(i) for i in input().split() if i != '-1'])
dic = Counter(a)
warshall_floyd(10)
ans = 0
for (key, val) in list(dic.items()):
	ans += d[key][1] * val
print(ans)


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)

def __starting_point():
	main()
__starting_point()


(H, W) = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
d = [0] * 10
for _ in range(H):
	for i in list(map(int, input().split())):
		if i >= 0:
			d[i] += 1
r = range(10)
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] * d[i] for i in r)))


(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for k in range(10)]
A = [list(map(int, input().split())) for k in range(H)]
for p in range(10):
	for q in range(10):
		for r in range(10):
			c[q][r] = min(c[q][r], c[q][p] + c[p][r])
ans = 0
for k in range(H):
	for l in range(W):
		if A[k][l] >= 0:
			ans += c[A[k][l]][1]
print(ans)


INF = float('inf')
(h, w) = map(int, input().split())
dist = [[0 for i in range(10)] for j in range(10)]
for i in range(10):
	s = list(map(int, input().split()))
	for j in range(10):
		if i == j:
			continue
		dist[i][j] = s[j]
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])
ans = 0
for i in range(h):
	T = list(map(int, input().split()))
	for j in range(w):
		if T[j] == -1:
			continue
		if T[j] == 1:
			continue
		ans += dist[T[j]][1]
print(ans)


(H, W) = list(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())))

def wf_func(mat):
	dist = mat
	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
dist = wf_func(c)
ans = 0
for i in range(H):
	for j in range(W):
		if A[i][j] != -1:
			ans += dist[A[i][j]][1]
print(ans)


(H, W) = map(int, input().split())
C = [list(map(int, input().split())) for j 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])
S = 0
for i in range(H):
	for x in list(map(int, input().split())):
		if x != -1:
			S += C[x][1]
print(S)


import copy
from itertools import product
import sys
sys.setrecursionlimit(10 ** 6)
(h, w) = list(map(int, input().split()))
map_ = [list(map(int, input().split())) for i in range(10)]
min_ = [[10 ** 4] * 10 for i in range(10)]

def dfs(start=0, now=0, value=0):
	for i in range(10):
		value_tmp = value + map_[now][i]
		if value_tmp < min_[start][i]:
			min_[start][i] = value_tmp
			dfs(start, now=i, value=value_tmp)
for i in range(10):
	dfs(start=i, now=i, value=0)
count = [0] * 10
for i in range(h):
	tmp = list(map(int, input().split()))
	for i in tmp:
		if i != -1:
			count[i] += 1
ans = 0
for i in range(10):
	ans += min_[i][1] * count[i]
print(ans)


(h, w) = map(int, input().split())
inf = 10 ** 20
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])
ans = 0
for _ in range(h):
	row = list(map(int, input().split()))
	for num in row:
		if num == -1:
			continue
		ans += d[num][1]
print(ans)


(h, w) = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
visited = {}
dist = {}
for i in range(10):
	dist[i] = 100000
dist[1] = 0
while len(dist) > 0:
	(cur, curDist) = sorted(dist.items(), key=lambda x: x[1])[0]
	del dist[cur]
	visited[cur] = curDist
	for i in range(10):
		if i == cur or i in visited:
			continue
		newDist = curDist + c[i][cur]
		dist[i] = min(dist[i], newDist)
cnts = [0] * 10
for _ in range(h):
	a = list(map(int, input().split()))
	for ai in a:
		if ai != -1:
			cnts[ai] += 1
cnt = 0
for i in range(10):
	cnt += cnts[i] * visited[i]
print(cnt)


(H, W) = map(int, input().split())
arr = [list(map(int, input().split())) for i in range(10)]
wall = [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):
			arr[j][k] = min(arr[j][i] + arr[i][k], arr[j][k])
ans = 0
for i in range(H):
	for item in wall[i]:
		if item < 0:
			continue
		ans += arr[item][1]
print(ans)


icase = 0
if icase == 0:
	(h, w) = list(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)


import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline
import math
from heapq import heappush, heappop

def solve(h, w, tables, A):
	for i in range(10):
		for j in range(10):
			for k in range(10):
				tables[j][k] = min(tables[j][k], tables[j][i] + tables[i][k])
	cost = 0
	for i in range(h):
		for j in range(w):
			p = A[i][j]
			if p == -1:
				continue
			cost += tables[p][1]
	return cost

def main():
	(h, w) = map(int, input().split(' '))
	tables = []
	for i in range(10):
		tables.append(list(map(int, input().split(' '))))
	A = []
	for _ in range(h):
		A.append(list(map(int, input().split(' '))))
	ans = solve(h, w, tables, A)
	print(ans)

def __starting_point():
	main()
__starting_point()


(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)]
from itertools import product
for (k, i, j) in product(range(10), repeat=3):
	if c[i][j] > c[i][k] + c[k][j]:
		c[i][j] = c[i][k] + c[k][j]
ans = 0
for (i, j) in product(range(H), range(W)):
	if A[i][j] != -1:
		ans += c[A[i][j]][1]
print(ans)


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


(h, w) = map(int, input().split())
C = [list(map(int, input().split())) for i in range(10)]
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])
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:
			ans += C[A[i][j]][1]
print(ans)


from itertools import *
(H, W) = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
for (i, j, k) in product(range(10), repeat=3):
	C[j][k] = min(C[j][k], C[j][i] + C[i][k])
l = [0] * 11
for _ in range(H):
	for i in input().split():
		l[int(i)] += 1
print(sum((C[i][1] * v for (i, v) in enumerate(l[:-1]))))


(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)]
N = 10
d = c
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])
ans = 0
for aa in A:
	for a in aa:
		if a != -1:
			ans += d[a][1]
print(ans)


import math
from math import gcd, pi, sqrt
INF = float('inf')
import sys
sys.setrecursionlimit(10 ** 6)
import itertools
from collections import Counter, deque

def i_input():
	return int(input())

def i_map():
	return list(map(int, input().split()))

def i_list():
	return list(i_map())

def i_row(N):
	return [i_input() for _ in range(N)]

def i_row_list(N):
	return [i_list() for _ in range(N)]

def s_input():
	return input()

def s_map():
	return input().split()

def s_list():
	return list(s_map())

def s_row(N):
	return [s_input for _ in range(N)]

def s_row_str(N):
	return [s_list() for _ in range(N)]

def s_row_list(N):
	return [list(s_input()) for _ in range(N)]

def main():
	(h, w) = i_map()
	c = [i_list() for i in range(10)]
	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):
		a = i_list()
		for b in a:
			if b >= 0:
				ans += c[b][1]
	print(ans)

def __starting_point():
	main()
__starting_point()