#include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }

constexpr ll mod = 1e9+7;
//const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int w,h; // 2<=w<=30,5<=h<=60
using P = pair<pair<int,int>,pair<int,int>>;        // time,turn,x,y
pii dy[2];


inline P make(int i,int j,int k,int l){
    return make_pair(make_pair(i,j),make_pair(k,l));
}

bool ok(int x,int y){
    if(x<0 or x>=h or y<0 or y>=w) return false;
    else return true;
}

int dist(int x1,int y1,int x2,int y2){
    return abs(x1-x2)+abs(y1-y2);
}


void solve(){
    vector<vector<char>> table(h,vector<char>(w));
    rep(i,h) rep(j,w) cin>>table[i][j];
    vector<pii> s,g;
    rep(i,h) rep(j,w){
        if(table[i][j]=='S'){
            s.push_back(pii(i,j));
        }
        if(table[i][j]=='T'){
            g.push_back(pii(i,j));
        }
    }
    vector<vector<vector<bool>>> use(2,vector<vector<bool>>(h,vector<bool>(w,true)));

    priority_queue<P,vector<P>,greater<P>> que;
    for(pii p : s){
        que.push(make(0,0,p.first,p.second));
        que.push(make(0,1,p.first,p.second));
    }


    while(!que.empty()){
        auto p = que.top();
        que.pop();
        int time = p.first.first;
        int turn = p.first.second;
        pii point = p.second;
        int x = point.first;
        int y = point.second;

        if(table[x][y]=='T'){
            cout << time << endl;
            return;
        }

        if(!use[turn][x][y]) continue;
        use[turn][x][y]=false;

 //       cout << "pop : " << time << " " << turn << " " << x << " " << y << endl;


        for(int i=-3;i<=3;i++){
            for(int j=dy[turn].first;j<dy[turn].second;j++){
                int nx=x+i;
                int ny=y+j;
                if(!ok(nx,ny)) continue;
                if(dist(x,y,nx,ny)>3 or !use[(turn+1)%2][nx][ny]) continue;
                if(table[nx][ny]=='X') continue;
                int ntime = time + table[nx][ny] - (table[nx][ny]=='T' ? 'T' : '0');
//                cout << "\tpush : " << ntime << " " << (turn+1)%2 << " " << nx << " " << ny << endl;
                que.push(make(ntime,(turn+1)%2,nx,ny));
            }
        }
    }

    cout << -1 << endl;
}

int main(){
    dy[0] = make_pair(1,4);
    dy[1] = make_pair(-3,0);
    while(cin>>w>>h){
        if(w==0&&h==0) break;
        solve();
    }
}

Measured memory_bytes: 3236