class Q1150
  @@field = nil;
  @@dp = nil;
  @@w = 0;
  @@h = 0;
  @@pos = Struct.new("Pos", :x, :y, :bRight)
  @@queue = Array.new
  LEFTFOOT = [[1,2],[1,1],[1,0],[1,-1],[1,-2],[2,1],[2,0],[2,-1],[3,0]]
  RIGHTFOOT = [[-1,2],[-1,1],[-1,0],[-1,-1],[-1,-2],[-2,1],[-2,0],[-2,-1],[-3,0]]
  MAX_COST = 100000000
  def climb x,y,bRight
    dir = bRight ? RIGHTFOOT : LEFTFOOT
    foot = bRight ? 1 : 0
    nfoot = bRight ? 0 : 1
    #puts "y = #{y}, x = #{x}, foot = #{foot}"
    dir.length.times do |i|
      nx = x + dir[i][0]
      ny = y + dir[i][1]
      if((0 <= nx && nx < @@w) && (0 <= ny && ny < @@h))
        if(@@field[ny][nx] == ?T)
          @@dp[ny][nx][nfoot] = (@@dp[y][x][foot] < @@dp[ny][nx][nfoot]) ? @@dp[y][x][foot] : @@dp[ny][nx][nfoot]
        elsif(?0 <= @@field[ny][nx] && @@field[ny][nx] <= ?9) 
          n = @@field[ny][nx].to_i
          if(@@dp[y][x][foot] + n < @@dp[ny][nx][nfoot])
            @@dp[ny][nx][nfoot] = @@dp[y][x][foot] + n
            #puts "dp[#{ny}][#{nx}][#{nfoot}] = #{@@dp[ny][nx][nfoot]}"
            @@queue.push @@pos.new(nx,ny,!bRight)
          end
        end
      end
    end
  end
  def doIt
    arg = gets.split(" ").map{|x| x.to_i}
    while(0 < arg[0] + arg[1])
      @@w = arg[0]
      @@h = arg[1]
      @@field = Array.new(@@h)
      @@h.times do |i|
        #puts  i
        @@field[i] = gets.split(" ")
      end
      @@dp = Array.new(@@h).map!{Array.new(@@w).map!{Array.new(2, MAX_COST)}}
      #climing simulation
      @@h.times do |j|
        @@w.times do |i|
          if(@@field[j][i] == ?S)
            #puts @@field[j][i]
            @@dp[j][i][0] = @@dp[j][i][1] = 0
            @@queue.push @@pos.new(i,j,true) 
            @@queue.push @@pos.new(i,j,false)
          end
        end     
      end
      #exec Dijkstra
      while(@@queue.length > 0)
        t = @@queue.shift
        climb t.x,t.y,t.bRight
      end
      #solve min
      min = MAX_COST
      @@h.times do |j|
        @@w.times do |i|
          if(@@field[j][i] == ?T)
            min = @@dp[j][i][0] < min ? @@dp[j][i][0] : min
            min = @@dp[j][i][1] < min ? @@dp[j][i][1] : min
          end
        end     
      end
      puts(min == MAX_COST ? -1 : min)
      arg = gets.split(" ").map{|x| x.to_i}
    end
  end
end

Q1150.new.doIt