def solve(HEIGHT, WIDTH, data):
from collections import defaultdict
edges = defaultdict(dict)
W = WIDTH
H = HEIGHT
for level in range(10):
for y in range(HEIGHT):
for x in range(WIDTH):
pos = x + y * WIDTH + level * WIDTH * HEIGHT
v = data[y][x]
if x < W - 1:
edges[pos + 1][pos] = 1
if x > 0:
edges[pos - 1][pos] = 1
if y < H - 1:
edges[pos + W][pos] = 1
if y > 0:
edges[pos - W][pos] = 1
if v == "S":
if level == 0:
start = pos
elif v == "G":
if level == 9:
goal = pos
else:
v = int(v)
if v == level:
edges[pos - W * H][pos] = 0
return one_to_one(start, goal, 10 * W * H, edges)