human
Write me a the Python code you requested given the following:
At 17:00, special agent Jack starts to escape from the enemy camp. There is a cliff in between the camp and the nearest safety zone. Jack has to climb the almost vertical cliff by stepping his feet on the blocks that cover the cliff. The cliff has slippery blocks where Jack has to spend time to take each step. He also has to bypass some blocks that are too loose to support his weight. Your mission is to write a program that calculates the minimum time to complete climbing.

Figure D-1 shows an example of cliff data that you will receive. The cliff is covered with square blocks. Jack starts cliff climbing from the ground under the cliff, by stepping his left or right foot on one of the blocks marked with 'S' at the bottom row. The numbers on the blocks are the "slippery levels". It takes t time units for him to safely put his foot on a block marked with t, where 1 ≤ t ≤ 9. He cannot put his feet on blocks marked with 'X'. He completes the climbing when he puts either of his feet on one of the blocks marked with 'T' at the top row.

Figure D-1: Example of Cliff Data

Jack's movement must meet the following constraints. After putting his left (or right) foot on a block, he can only move his right (or left, respectively) foot. His left foot position (lx, ly) and his right foot position (rx, ry) should satisfy lx < rx
and | lx - rx | + | ly - ry | ≤ 3
. This implies that, given a position of his left foot in Figure D-2 (a), he has to place his right foot on one of the nine blocks marked with blue color. Similarly, given a position of his right foot in Figure D-2 (b), he has to place his left foot on one of the nine blocks marked with blue color.

Figure D-2: Possible Placements of Feet


Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows:

> w h
>  s(1,1) ... s(1,w)
>  s(2,1) ... s(2,w)
>  ...
>  s(h,1) ... s(h,w)
>

The integers w and h are the width and the height of the matrix...
assistant
#Here is the Python code you requested.

from heapq import heappush, heappop

R_next = tuple((x, y) for x in range(-3, 0) for y in range(-2, 3) if abs(x) + abs(y) <= 3)
L_next = tuple((x, y) for x in range(1, 4) for y in range(-2, 3) if abs(x) + abs(y) <= 3)
c_num = tuple(str(i) for i in range(10))
L, R = 0, 1
INF = 10 ** 20

def conv(c):
  if c in c_num:
    return int(c)
  if c in ("T", "S"):
    return 0
  return -1

while True:
  w, h = map(int, input().split())
  if w == 0:
    break
  mp = [[-1] * 3 + input().split() + [-1] * 3 for _ in range(h)]
  for i in range(h):
    mp[i] = list(map(conv, mp[i]))
  mp.insert(0, [-1] * (w + 6))
  mp.insert(0, [-1] * (w + 6))
  mp.append([-1] * (w + 6))
  mp.append([-1] * (w + 6))

  def search():
    start = []
    goal = []
    for x in range(3, w + 3):
      if mp[h + 1][x] == 0:
        start.append((x, h + 1))
      if mp[2][x] == 0:
        goal.append((x, 2))

    que = []
    dic = {}
    for x, y, in start:
      heappush(que, (0, x, y, L))
      heappush(que, (0, x, y, R))
      dic[(x, y, L)] = 0
      dic[(x, y, R)] = 0
 
    while que:
      total, x, y, foot = heappop(que)
      if foot == R:
        direct = R_next
        next_foot = L
      else:
        direct = L_next
        next_foot = R

      for dx, dy in direct:
        nx, ny = x + dx, y + dy
        if (nx, ny) in goal:
          return total
        cost = mp[ny][nx]
        if cost == -1:
          continue
        if not (nx, ny, next_foot) in dic or dic[(nx, ny, next_foot)] > total + cost:
          dic[(nx, ny, next_foot)] = total + cost
          heappush(que, (total + cost, nx, ny, next_foot))
    else:
      return -1
  print(search())