Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
sys.setrecursionlimit(10000)

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
us = [str(i) for i in range(N, 0, -1)]
queue = deque([])
dx = [1,0,-1,0]
dy = [0,1,0,-1]

def search(posi):
	global flag
	global us
	global count
	global queue

	x, y, c = posi
	if (field[y][x] == us[-1]):
		flag = True
		if (us[-1] == str(N)):
			print(c)
			exit()
		us.pop()
		queue.clear()
		queue.append([x, y, c])
		return

	q_count = 0
	for q in range(4):
		nx = x + dx[q]
		ny = y + dy[q]
		if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and ([nx,ny] not in queue) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
			queue.append([nx, ny, c+1])
			q_count += 1
	for i in range(q_count):
		if (flag):
			return
		search(queue.popleft())


bf = False
for i in range(H):
	for j in range(W):
		if field[i][j] == 'S':
			c = 0
			queue.append([j, i, c])
			bf = True
			break
	if (bf):
		break

while 1:
	flag = False
	search(queue.popleft())


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
sys.setrecursionlimit(10000)

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
us = [str(i) for i in range(N, 0, -1)]
queue = deque([])
dx = [1,0,-1,0]
dy = [0,1,0,-1]

def search():
	global flag
	global us
	global count
	global queue

	x, y, c = queue.popleft()
	
	if (field[y][x] == us[-1]):
		flag = True
		if (us[-1] == str(N)):
			print(c)
			exit()
		us.pop()
		queue.clear()
		queue.append([x, y, c])
		return

	for q in range(4):
		nx = x + dx[q]
		ny = y + dy[q]
		if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and ([nx,ny] not in queue) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
			queue.append([nx, ny, c+1])


bf = False
for i in range(H):
	for j in range(W):
		if field[i][j] == 'S':
			c = 0
			queue.append([j, i, c])
			bf = True
			break
	if (bf):
		break

while 1:
	flag = False
	search()


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
us = [str(i) for i in range(N, 0, -1)]
queue = deque([])
dx = [1,0,-1,0]
dy = [0,1,0,-1]

def search():
	global flag
	global us
	global count
	global queue

	while queue:
		x, y, c = queue.popleft()
		
		if (field[y][x] == us[-1]):
			flag = True
			if (us[-1] == str(N)):
				print(c)
				exit()
			us.pop()
			queue.clear()
			queue.append([x, y, c])
			return
	
		for q in range(4):
			nx = x + dx[q]
			ny = y + dy[q]
			if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and ([nx,ny] not in queue) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
				queue.append([nx, ny, c+1])


bf = False
for i in range(H):
	for j in range(W):
		if field[i][j] == 'S':
			c = 0
			queue.append([j, i, c])
			bf = True
			break
	if (bf):
		break

while 1:
	flag = False
	search()


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = deque()

def search(start, goal):
	Q.append(start)
	while Q:
		x, y, c = Q.popleft()
		searched[y][x] = 1
		
		if ([x, y, 0] == goal):
			return c
		
		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
				Q.append([nx, ny, c+1])

for i in range(H):
	for j in range(W):
		if field[i][j] in 'S123456789':
			node_posi[field[i][j]] = [j, i, 0]

s =	'S123456789'
cost = 0
for i in range(len(node_posi)-1):
	Q.clear()
	searched = [[0]*W for i in range(H)]
	cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = deque()

def search(start, goal):
	Q.append(start)
	while Q:
		x, y, c = Q.popleft()
		searched[y][x] = 1
		
		if ([x, y, 0] == goal):
			return c
		
		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
				Q.append([nx, ny, c+1])

for i in range(H):
	for j in range(W):
		if field[i][j] in 'S123456789':
			node_posi[field[i][j]] = [j, i, 0]

s =	'S123456789'
cost = 0
for i in range(len(node_posi)-1):
	Q.clear()
	searched = [[0]*W for i in range(H)]
	cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]

starts[0] = "S"
for y in range(h):
	for x in range(w):
		if stage[y][x] in starts:
			starts_y[starts.index(stage[y][x])] = y
			starts_x[starts.index(stage[y][x])] = x
		if stage[y][x] in goals:
			goals_y[goals.index(stage[y][x])] = y
			goals_x[goals.index(stage[y][x])] = x

sum = 0

for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
	bfs_map = [[-1 for j in range(w)] for i in range(h)]
	data_y = [start_y]
	data_x = [start_x]
	bfs_map[start_y][start_x] = 0
	goal = False
	
	while len(data_y) != 0 and not goal:
		y = data_y.pop(0)
		x = data_x.pop(0)
		goal = False
	
		for i in range(4):
			y += [1,-1,0,0][i]
			x += [0,0,1,-1][i]
			
			if y >= 0 and y < h and x >= 0 and x < w:
				if bfs_map[y][x] == -1 and stage[y][x] != "X":
					bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
					data_y.append(y)
					data_x.append(x)
				if bfs_map[goal_y][goal_x] != -1:
					sum += bfs_map[goal_y][goal_x]
					goal = True
					break
			y -= move_y[i]
			x -= move_x[i]

print(sum)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]

starts[0] = "S"
for y in range(h):
	for x in range(w):
		if stage[y][x] in starts:
			starts_y[starts.index(stage[y][x])] = y
			starts_x[starts.index(stage[y][x])] = x
		if stage[y][x] in goals:
			goals_y[goals.index(stage[y][x])] = y
			goals_x[goals.index(stage[y][x])] = x

sum = 0

for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
	bfs_map = [[-1 for j in range(w)] for i in range(h)]
	data_y = [start_y]
	data_x = [start_x]
	bfs_map[start_y][start_x] = 0
	goal = False
	
	while len(data_y) != 0 and not goal:
		y = data_y.pop(0)
		x = data_x.pop(0)
		goal = False
	
		for i in range(4):
			y += [1,-1,0,0][i]
			x += [0,0,1,-1][i]
			
			if y >= 0 and y < h and x >= 0 and x < w:
				if bfs_map[y][x] == -1 and stage[y][x] != "X":
					bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
					data_y.append(y)
					data_x.append(x)
				if bfs_map[goal_y][goal_x] != -1:
					sum += bfs_map[goal_y][goal_x]
					goal = True
					break
			y -= move_y[i]
			x -= move_x[i]

print(sum)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]

starts[0] = "S"
for y in range(h):
	for x in range(w):
		if stage[y][x] in starts:
			starts_y[starts.index(stage[y][x])] = y
			starts_x[starts.index(stage[y][x])] = x
		if stage[y][x] in goals:
			goals_y[goals.index(stage[y][x])] = y
			goals_x[goals.index(stage[y][x])] = x

move_x = [0,0,1,-1]
sum = 0

for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
	bfs_map = [[-1 for j in range(w)] for i in range(h)]
	data_y = [start_y]
	data_x = [start_x]
	bfs_map[start_y][start_x] = 0
	goal = False
	
	while len(data_y) != 0 and not goal:
		y = data_y.pop(0)
		x = data_x.pop(0)
		goal = False
	
		for i in range(4):
			y += [1,-1,0,0][i]
			x += move_x[i]
			
			if y >= 0 and y < h and x >= 0 and x < w:
				if bfs_map[y][x] == -1 and stage[y][x] != "X":
					bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
					data_y.append(y)
					data_x.append(x)
				if bfs_map[goal_y][goal_x] != -1:
					sum += bfs_map[goal_y][goal_x]
					goal = True
					break
			y -= move_y[i]
			x -= move_x[i]

print(sum)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = deque()

def search(start, goal):
	Q.append(start)
	while Q:
		x, y, c = Q.popleft()
		searched[y][x] = 1
		
		if ([x, y, 0] == goal):
			return c
		
		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
				Q.append([nx, ny, c+1])

for i in range(H):
	for j in range(W):
		if field[i][j] in 'S123456789':
			node_posi[field[i][j]] = [j, i, 0]

s =	'S123456789'
cost = 0
for i in range(len(node_posi)-1):
	Q.clear()
	searched = [[0]*W for i in range(H)]
	cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = []

def search(start, goal):
	Q.append(start)
	while Q:
		x, y, c = Q.pop(0)
		searched[y][x] = 1
		
		if ([x, y, 0] == goal):
			return c
		
		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
				Q.append([nx, ny, c+1])

for i in range(H):
	for j in range(W):
		if field[i][j] in 'S123456789':
			node_posi[field[i][j]] = [j, i, 0]

s =	'S123456789'
cost = 0
for i in range(len(node_posi)-1):
	Q.clear()
	searched = [[0]*W for i in range(H)]
	cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)

Buggy python submission (verdict: Runtime Error):
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = []

for i in range(H):
	for j in range(W):
		if field[i][j] in 'S123456789':
			node_posi[field[i][j]] = [j, i, 0]

s =	'S123456789'
cost = 0
for i in range(len(node_posi)-1):
	Q.clear()
	searched = [[0]*W for i in range(H)]
	start, goal = node_posi[s[i]], node_posi[s[i+1]]
	Q.append(start)
	while Q:
		x, y, c = Q.pop(0)
		searched[y][x] = 1
		
		if ([x, y, 0] == goal):
			cost += c
			break
		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
				Q.append([nx, ny, c+1])
print(cost)


Fixed submission:
#import time, sys#
from collections import deque

H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''

#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]

#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
	for j in range(W):
		if field[i][j] in nodes:
			node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#

cost = 0
#count = 1#
for i in range(len(node_posi)-1):
	#s2 = time.time()#
	searched = [[-1]*W for i in range(H)]
	start, goal = node_posi[i], node_posi[i+1]
	Q = deque([start])
	searched[start[1]][start[0]] = 0
	goal_flag = False
	
	while Q and not goal_flag:
		x, y = Q.popleft()
		goal_flag = False

		for q in range(4):
			nx = x + [1,0,-1,0][q]
			ny = y + [0,1,0,-1][q]
			
			if nx >= 0 and nx < W and ny >= 0 and ny < H:
				if searched[ny][nx] == -1 and field[ny][nx] != 'X':
					Q.append([nx, ny])
					searched[ny][nx] = searched[y][x] + 1
					if ([nx, ny] == goal):
						cost += searched[ny][nx]
						goal_flag = True
						break

			#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
			#sys.stdout.write("\r{}".format(s))#
			#sys.stdout.flush()#
	
	#e2 = time.time()#
	#print("{}:{}".format(count, e2-s2))#
	#count += 1#
print(cost)