Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys

H, W = map(int, input().split())
a = [[] for i in range(10)]
for i in range(10):
	c = [int(j) for j in input().split()]
	for j in range(10):
		a[i].append([c[j], j])
ans = 0
for i in range(H):
	A = [int(j) for j in input().split()]
	for j in A:
		if j == -1:
			continue
		b = [[0, j]]
		c = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(b):
			e = heapq.heappop(b)
			c[e[1]] = True
			for k in a[e[1]]:
				if c[k[1]]:
					continue
				if d[e[1]] + k[0] < d[k[1]]:
					d[k[1]] = d[e[1]] + k[0]
					heapq.heappush(b, [d[k[1]], k[1]])
		ans += d[1]
print(ans)

Fixed submission:
import heapq
import sys

H, W = map(int, input().split())
c = [[int(j) for j in input().split()] for i in range(10)]
A = [[int(j) for j in input().split()] for i in range(H)]
ans = 0
for i in A:
	for j in i:
		if j == -1:
			continue
		a = [(0, j)]
		b = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(a):
			e = heapq.heappop(a)
			b[e[1]] = True
			for k in range(len(c[e[1]])):
				if b[k]:
					continue
				if d[e[1]] + c[e[1]][k] < d[k]:
					d[k] = d[e[1]] + c[e[1]][k]
					heapq.heappush(a, (d[k], k))
		ans += d[1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys

H, W = map(int, input().split())
a = [[] for i in range(10)]
for i in range(10):
	c = [int(i) for i in input().split()]
	for j in range(10):
		a[i].append([c[j], j])
ans = 0
for i in range(H):
	A = [int(i) for i in input().split()]
	for j in A:
		if j == -1:
			continue
		b = [[0, j]]
		c = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(b):
			e = heapq.heappop(b)
			c[e[1]] = True
			for k in a[e[1]]:
				if c[k[1]]:
					continue
				if d[e[1]] + k[0] < d[k[1]]:
					d[k[1]] = d[e[1]] + k[0]
					heapq.heappush(b, [d[k[1]], k[1]])
		ans += d[1]
print(ans)

Fixed submission:
import heapq
import sys

H, W = map(int, input().split())
c = [[int(j) for j in input().split()] for i in range(10)]
A = [[int(j) for j in input().split()] for i in range(H)]
ans = 0
for i in A:
	for j in i:
		if j == -1:
			continue
		a = [(0, j)]
		b = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(a):
			e = heapq.heappop(a)
			b[e[1]] = True
			for k in range(len(c[e[1]])):
				if b[k]:
					continue
				if d[e[1]] + c[e[1]][k] < d[k]:
					d[k] = d[e[1]] + c[e[1]][k]
					heapq.heappush(a, (d[k], k))
		ans += d[1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys

H, W = map(int, input().split())
a = [[] for i in range(10)]
for i in range(10):
	c = [int(i) for i in input().split()]
	for j in range(10):
		a[i].append([c[j], j])
ans = 0
for i in range(H):
	A = [int(i) for i in input().split()]
	for j in A:
		if j == -1:
			continue
		b = [[0, j]]
		c = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(b):
			e = heapq.heappop(b)
			c[e[1]] = True
			for k in a[e[1]]:
				if c[k[1]]:
					continue
				if d[e[1]] + k[0] < d[k[1]]:
					d[k[1]] = d[e[1]] + k[0]
					heapq.heappush(b, [d[k[1]], k[1]])
		ans += d[1]
print(ans)

Fixed submission:
import heapq
import sys

H, W = map(int, input().split())
c = [[int(j) for j in input().split()] for i in range(10)]
A = [[int(j) for j in input().split()] for i in range(H)]
ans = 0
for i in A:
	for j in i:
		if j == -1:
			continue
		a = [(0, j)]
		b = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(a):
			e = heapq.heappop(a)
			b[e[1]] = True
			for k in range(len(c[e[1]])):
				if b[k]:
					continue
				if d[e[1]] + c[e[1]][k] < d[k]:
					d[k] = d[e[1]] + c[e[1]][k]
					heapq.heappush(a, (d[k], k))
		ans += d[1]
print(ans)