#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
// #define USE_LLONG_AS_INT
#ifdef USE_LLONG_AS_INT
#define int long long
#define inf (1ll<<60)
#else
#define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=(n)-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

int w,h;
int d[60][30][2];
char s[60][30];
struct point{
    int y,x,f;
    point(int y,int x,int f):y(y),x(x),f(f){};
    bool operator<(const point& a)const{
        return true;
    }
};

using P=pair<int,point>;

bool is_valid(int y,int x){
    return 0<=y&&y<h&&0<=x&&x<w;
}

void run(){
    cin>>w>>h;
    if(!w)return;
    rep(i,h)rep(j,w)rep(k,2)d[i][j][k]=inf;
    priority_queue<P,vector<P>,greater<P>> q;
    rep(i,h)rep(j,w){
        cin>>s[i][j];
        if(s[i][j]=='S'){
            q.push(mp(0,point(i,j,0)));
            q.push(mp(0,point(i,j,1)));
            d[i][j][0]=d[i][j][1]=0;
        }
    }
    int ans=inf;
    while(!q.empty()){
        P p=q.top();
        q.pop();
        point v=p.se;
        if(d[v.y][v.x][v.f]<p.fi)continue;
        int dy[]={-2,-1,-1,0,0,0,1,1,2},dx[]={1,1,2,1,2,3,1,2,1};
        rep(i,9){
            int y=v.y+dy[i],x=v.x+(v.f?-dx[i]:dx[i]),f=!v.f;
            if(!is_valid(y,x))continue;
            if(s[y][x]=='T')ans=min(ans,p.fi);
            else{
                int c;
                if(s[y][x]=='S')c=0;
                else if(s[y][x]=='X')c=inf;
                else c=s[y][x]-'0';
                if(d[y][x][f]>d[v.y][v.x][v.f]+c){
                    d[y][x][f]=d[v.y][v.x][v.f]+c;
                    q.push(mp(d[y][x][f],point(y,x,f)));
                }
            }
        }
    }
    cout<<(ans!=inf?ans:-1)<<endl;
    run();
}
