#include<map>
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
#include<cctype>
#include<iomanip>
#include<vector>
#include<queue>
 
using namespace std;
#define REP(i,b,e) for(int i=(int)b;i<(int)e;i++)
#define rep0(i,n) REP(i,0,n)
#define rep1(i,n) REP(i,1,n+1)
#define shosu setprecision(10)
typedef long long ll;
typedef pair<int,pair<int,pair<int,int>>> P;

ll longinf=1ll<<60;
int inf=1<<29;
int dy[]={2,1,1,0,0,0,-1,-1,-2};
int dlx[]={1,1,2,1,2,3,1,2,1}; //0~8
int drx[]={-1,-1,-2,-1,-2,-3,-1,-2,-1};

int W,H;
char s[62][32];
int dist[62][32][2];

//左足0,右足1
//queには距離,足,点を入れる
int main(){
    while(1){
        int ans=inf;
        cin>>W>>H;
        if(W==0&&H==0) break;
        rep0(h,H)rep0(w,W) cin>>s[h][w];
        rep0(h,H)rep0(w,W)rep0(f,2) dist[h][w][f]=inf;
        priority_queue<P,vector<P>,greater<P>> que;
        rep0(h,H){
            rep0(w,W){
                if(s[h][w]=='S'){
                    P x,y;
                    x={0,{0,{h,w}}};
                    y={0,{1,{h,w}}};
                    dist[h][w][0]=dist[h][w][1]=0;
                    que.push(x);
                    que.push(y);
                }
            }
        }
        while(que.size()){
            P x;
            x=que.top();
            que.pop();
            int d=x.first;
            int foot=x.second.first;
            int h=x.second.second.first;
            int w=x.second.second.second;
                if(foot){
                    rep0(i,9){
                        int nh=h+dy[i];
                        int nw=w+dlx[i];
                        if(nh>=0&&nw>=0&&nh<H&&nw<W){
                            if(s[nh][nw]=='X') continue;
                            else if(s[nh][nw]=='T') ans=min(ans,dist[h][w][1]);
                            else if(s[nh][nw]=='S') continue;
                            else{
                                int n=s[nh][nw]-'0';
                                if(dist[nh][nw][0]<dist[h][w][1]+n) continue;
                                dist[nh][nw][0]=dist[h][w][1]+n;
                                P nx;
                                nx={dist[nh][nw][0],{0,{nh,nw}}};
                                que.push(nx);
                            }
                        }
                    }
                }
                else{
                    rep0(i,9){
                        int nh=h+dy[i];
                        int nw=w+drx[i];
                        if(nh>=0&&nw>=0&&nh<H&&nw<W){
                            if(s[nh][nw]=='X') continue;
                            else if(s[nh][nw]=='T') ans=min(ans,dist[h][w][0]);
                            else if(s[nh][nw]=='S') continue;
                            else{
                                int n=s[nh][nw]-'0';
                                if(dist[nh][nw][1]<dist[h][w][0]+n) continue;
                                dist[nh][nw][1]=dist[h][w][0]+n;
                                P nx;
                                nx={dist[nh][nw][1],{1,{nh,nw}}};
                                que.push(nx);
                            }
                        }
                    }
                }
            
        }
        /*rep0(i,2){
        rep0(h,H){
            rep0(w,W){
                cout<<dist[h][w][i]<<" ";
            };
            cout<<endl;
        }
        cout<<endl;
        }*/
        if(ans==inf) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


Measured memory_bytes: 4408