#include<complex>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<deque>
#include<queue>
#include<climits>
#include<list>
#include<stack>
#include<ctime>
#define all(n) n.begin(),n.end()
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define insert(a,b,c,d) PIB(P(a,b),IB(c,d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define INF (1<<27)
#define EPS 1e-10
#define equals(a,b) (fabs((a)-(b)) < EPS)

using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef pair<int,bool> IB;
typedef pair<P,IB> PIB;
typedef unsigned long long ull;
typedef long long ll;
const int left_dx[9] = {-1,-1,-1,-1,-1,-2,-2,-2,-3};
const int left_dy[9] = {-2,-1,+0,+1,+2,-1,+0,+1,+0};
const int right_dx[9] = {+1,+1,+1,+1,+1,+2,+2,+2,+3};
const int right_dy[9] = {-2,-1,0,+1,+2,-1,+0,+1,+0};


class Solve{
 private:
  int w,h;
  int d[61][31][2]; // 0->left, 1->right
  char cmap[61][31];
 public:
  Solve(int w,int h):w(w),h(h){
    rep(i,h)rep(j,w)rep(k,2)d[i][j][k] = INT_MAX;
  }
  ~Solve(){}

  void input(){
    rep(i,h)
      rep(j,w)
      cin >> cmap[i][j];
  }

  void printAll(){
    cout << endl << "### START ###" << endl;
    cout << "w = " << w << ",h = " << h << endl;
    cout << "cmap --- " << endl;
    rep(i,h){
      rep(j,w){
	cout << cmap[i][j] << " ";
      }
      cout << endl;
    }
    cout << endl;
    cout << "d_left --- "<< endl;
    rep(i,h){
      rep(j,w){
	if(d[i][j][0] != INT_MAX)cout << d[i][j][0] << ' ';
	else cout << "0 ";
      }
      cout << endl;
    }

    cout << endl << "d_right --- "<< endl;
    rep(i,h){
      rep(j,w){
	if(d[i][j][1] != INT_MAX)cout << d[i][j][1] << ' ';
	else cout << "0 ";
      }
      cout << endl;
    }
    cout << endl << "comb---" << endl;
    rep(i,h){
      rep(j,w){
	if(d[i][j][0] == INT_MAX && d[i][j][1] != INT_MAX)cout << d[i][j][1] << ' ';
	else if(d[i][j][0] != INT_MAX && d[i][j][1] == INT_MAX)cout << d[i][j][0] << " ";
	else if(d[i][j][0] != INT_MAX && d[i][j][1] != INT_MAX)cout << min(d[i][j][0],d[i][j][1]) << ' ';
	else cout << 0 << ' ';
      }
      cout << endl;
    }
    cout << "### END ###" << endl << endl;
  }

  void run(){
    input();
    deque<PIB> deq; // bool true->left,false->right
    rep(i,w){
      if(cmap[h-1][i] != 'S')continue;
      d[h-1][i][1] = d[h-1][i][0] = 0; // %%%
      deq.pb(insert(i,h-1,0,1));      
      deq.pb(insert(i,h-1,0,0));
    }

    while(!deq.empty()){
      PIB pib = deq.front(); deq.pop_front();
      if(cmap[pib.F.S][pib.F.F] == 'T'){
	int lr = pib.S.S==0?0:1;
	d[pib.F.S][pib.F.F][lr] = min(d[pib.F.S][pib.F.F][lr],pib.S.F);
	continue;
      }
     

      P p = pib.F;
      int LR = pib.S.S==0?0:1;
      int rLR = LR == 0?1:0;
      rep(i,9){
	int nx,ny;
	if(LR == 0)nx = p.F+right_dx[i],ny = p.S+right_dy[i];
	else nx = p.F+left_dx[i],ny = p.S+left_dy[i];

	if(!(0<= nx && nx < w && 0<= ny && ny < h))continue;
	if(cmap[ny][nx] == 'X')continue;
	int cost;
	if(cmap[ny][nx] == 'T')cost = 0;
	else cost = (cmap[ny][nx]-'0');
	if(d[ny][nx][rLR] <= cost+pib.S.F)continue;
	d[ny][nx][rLR] =  cost+pib.S.F;
	if(deq.front().S.F < d[ny][nx][rLR])deq.pb(insert(nx,ny,d[ny][nx][rLR],rLR));
	else deq.pf(insert(nx,ny,d[ny][nx][rLR],rLR));  
 
      }
    }

    output();

  }
  
  void output(){
    int MIN = INT_MAX;
    rep(i,w){
      if(cmap[0][i] == 'T')MIN = min(MIN,min(d[0][i][1],d[0][i][0]));
    }
    if(MIN != INT_MAX)cout << MIN << endl;
    else cout << -1 << endl;
  }


};

int main(){
  int _w,_h;
  while(cin >> _w >> _h && _w+_h){
    Solve ans = Solve(_w,_h);
    ans.run();
    //ans.printAll();
  }
return 0;
}

Measured memory_bytes: 1260