#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vt = vector<T>;
template<class T> using vvt = vector<vt<T>>;
template<class T> using ttt = tuple<T,T>;
using tii = tuple<int,int>;
using vi = vector<int>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define DIV int(1e9+7)
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-8;
//const double PI = M_PI;
inline ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}
inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;}
#define chmax(a,b) a=max(a,b)

/*Coding Space*/
const short INF = 1800*10;
using tiiii = tuple<int,int,int,int>;
int main(){
  int w,h;
  while(cin >> w >> h , w){
    priority_queue<tuple<short,bool,short,short,short,short>,vt<tuple<short,bool,short,short,short,short>>,greater<tuple<short,bool,short,short,short,short>> > q;
    unordered_map<int,unordered_map<int,int>> c;
    rep(i,80)rep(j,80)  c[i][j] = 0;
    rep(i,h)rep(j,w)  {char cc; cin >> cc;
      if(cc == 'X')  c[h-i-1][j] = 0;
      else if(cc == 'T')  c[h-i-1][j] = -INF;
      else if(cc == 'S'){
        c[h-i-1][j] = INF;
        q.push(tuple<short,bool,short,short,short,short>{0,1,-100,-100,h-i-1,j});
        q.push(tuple<short,bool,short,short,short,short>{0,0,h-i-1,j,-100,-100});
        q.push(tuple<short,bool,short,short,short,short>{0,0,-100,-100,h-i-1,j});
        q.push(tuple<short,bool,short,short,short,short>{0,1,h-i-1,j,-100,-100});
      }
      else c[h-i-1][j] = cc - '0';
    }
    map<tuple<short,short,short,short>,short> min_table;
    bool is_ans = 0;
    while(!q.empty()){
      bool lr;
      short a,lw,lh,rw,rh; tie(a,lr,lh,lw,rh,rw) = q.top(); q.pop();
      //cerr << a << " " << lh << " " << lw << " " << rh << " " << rw << " " << q.size() << endl;
      //cerr << c[lh][lw] << " " << c[rh][rw] << endl;
      if(c[lh][lw] == -INF){
        cout << a << endl;
        is_ans = 1;
        break;
      }
      if(c[rh][rw] == -INF){
        cout << a << endl;
        is_ans = 1;
        break;
      }
      //cerr << min_table.size() << endl;
      if(lr){
        for(short d = -2; d <= 2; d++)for(short dd = 1; dd <= 3; ++dd){
          if(abs(d)+abs(dd) >= 4)continue;
          short nh = rh + d,nw = rw - dd;
          if(nh < 0 || nw < 0 || rh < 0 || rw < 0)  continue;
          if(c[nh][nw] == 0)  continue;
          short new_c = a + c[nh][nw];
          if(min_table.count(tiiii{nh,nw,rh,rw})){
            if(min_table[tiiii{nh,nw,rh,rw}] > new_c){
              min_table[tiiii{nh,nw,rh,rw}] = new_c;
              if(c[nh][nw] == -INF)            q.push(tuple<short,bool,short,short,short,short>{a,0,nh,nw,rh,rw});
              else q.push(tuple<short,bool,short,short,short,short>{new_c,0,nh,nw,rh,rw});            }else{
;
            }
          }else{
            min_table[tiiii{nh,nw,rh,rw}] = new_c;
            if(c[nh][nw] == -INF)            q.push(tuple<short,bool,short,short,short,short>{a,0,nh,nw,rh,rw});
            else q.push(tuple<short,bool,short,short,short,short>{new_c,0,nh,nw,rh,rw});
          }
        }
      }else{
        for(short d = -2; d <= 2; d++)for(short dd = 1; dd <= 3; ++dd){
          if(abs(d)+abs(dd) >= 4)continue;
          short nh = lh + d,nw = lw + dd;
          if(nh < 0 || nw < 0 || lh < 0 || lw < 0)  continue;
          if(c[nh][nw] == 0)  continue;
          short new_c = a + c[nh][nw];
          if(min_table.count(tiiii{lh,lw,nh,nw})){
            if(min_table[tiiii{lh,lw,nh,nw}] > new_c){
              min_table[tiiii{lh,lw,nh,nw}] = new_c;
              if(c[nh][nw] == -INF)q.push(tuple<short,bool,short,short,short,short>{a,1,lh,lw,nh,nw});
              else q.push(tuple<short,bool,short,short,short,short>{new_c,1,lh,lw,nh,nw});
            }else{
;
            }
          }else{
            min_table[tiiii{lh,lw,nh,nw}] = new_c;
            if(c[nh][nw] == -INF)q.push(tuple<short,bool,short,short,short,short>{a,1,lh,lw,nh,nw});
            else q.push(tuple<short,bool,short,short,short,short>{new_c,1,lh,lw,nh,nw});
          }
        }
      }
    }
    if(!is_ans){cout << -1 << endl;}
  }
}



Measured memory_bytes: 3908