#include <bits/stdc++.h>
using namespace std;

const int W = 30;
const int H = 60;
const int INF = 1 << 29;

struct foot{
  int ly, lx, ry, rx;
  foot(int ly=0, int lx=0, int ry=0, int rx=0):ly(ly),lx(lx),ry(ry),rx(rx){}
};

struct state{
  foot p;
  int c;
  bool isRight;
  state(foot p=foot(0,0,0,0), bool ir=true, int c=0):p(p),c(c),isRight(ir){}
  bool operator < (const state &s) const {
    return c > s.c;
  }
};

int w,h,v[H][W];

bool check(const int &y, const int &x){
  if(y < 0 || y >= h) return false;
  if(x < 0 || x >= w) return false;
  return v[y][x] != -1;
}

vector<foot> nextFoot(const state &s){
  vector<foot> res;

  for(int i=1;i<=3;i++){
    for(int j=-3+i;j<=3-i;j++){
      if(s.isRight){
        if(!check(s.p.ly+j, s.p.lx+i)) continue;
        res.push_back(foot(s.p.ly, s.p.lx, s.p.ly+j, s.p.lx+i));
      } else {
        if(!check(s.p.ry+j, s.p.rx-i)) continue;
        res.push_back(foot(s.p.ry+j, s.p.rx-i, s.p.ry, s.p.rx));
      }
    }
  }

  return res;
}

  int d[H][W][H][W];

int solve(){
  priority_queue<state> q;
  state u,u2;  
  vector<foot> next;

  for(int i=0;i<h*w;i++) for(int j=0;j<h*w;j++) d[i/w][i%w][j/w][j%w] = INF;

  for(int i=0;i<w;i++){
    if(v[h-1][i] != -3) continue;
    u = state(foot(h-1, i, h-1, i), true);
    next = nextFoot(u);
    for(int j=0;j<next.size();j++){
      if(v[next[j].ry][next[j].rx] < 0) q.push(state(next[j], false));
      else q.push(state(next[j], false, v[next[j].ry][next[j].rx]));
    }
    
    u.isRight = false;
    next = nextFoot(u);
    for(int j=0;j<next.size();j++){
      if(v[next[j].ly][next[j].lx] < 0) q.push(state(next[j], true));
      else q.push(state(next[j], true, v[next[j].ly][next[j].lx]));
    }
  }
  
  while(!q.empty()){
    u = q.top();
    q.pop();

    if(v[u.p.ly][u.p.lx] == -2 || v[u.p.ry][u.p.rx] == -2){
      return u.c;
    }

    //cout << u.p.ly << ' ' << u.p.lx << ' ' << u.p.ry << ' ' << u.p.rx << ' ' << u.c << endl;

    next = nextFoot(u);
    for(int i=0;i<next.size();i++){
      if(u.isRight){
        u2 = state(next[i], !u.isRight);
        if(v[u2.p.ry][u2.p.rx] < 0) u2.c = u.c;
        else u2.c = u.c + v[u2.p.ry][u2.p.rx];
      } else {
        u2 = state(next[i], !u.isRight);
        if(v[u2.p.ly][u2.p.lx] < 0) u2.c = u.c;
        else u2.c = u.c + v[u2.p.ly][u2.p.lx];
      }
      if(d[u2.p.ly][u2.p.lx][u2.p.ry][u2.p.rx] <= u2.c) continue;
      d[u2.p.ly][u2.p.lx][u2.p.ry][u2.p.rx] = u2.c;
      q.push(u2);
    }
  }

  return -1;
}

int main(){
  while(cin >> w >> h && (w|h)){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        char c;
        cin >> c;
        if(c == 'S') v[i][j] = -3;
        else if(c == 'T') v[i][j] = -2;
        else if(c == 'X') v[i][j] = -1;
        else v[i][j] = c - '0';
      }
    }
    cout << solve() << endl;
  }
}