#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

int H,W,inf = 1e9;
int F[70][40];
int dp[70][40][70][40][2] = {};
vector<pair<int,int>> goal;

bool in(int x,int y){
    return 1<=x && x<=H && 1<=y && y<=W;
}
struct state{
    int time;
    int lx,ly,rx,ry;
    bool move = false;
    bool operator<(const state &right)const{
        return time > right.time;
    }
};
int main(){
    while(cin >> W >> H && H>0){
        char c;
        for(int i=0;i<=H;i++){
            for(int j=0;j<=W;j++){
                for(int k=0;k<=H;k++){
                    for(int l=0;l<=W;l++){
                        dp[i][j][k][l][0] = inf;
                        dp[i][j][k][l][1] = inf;
                    }
                }
            }
        }
        priority_queue<state> Q;
        for(int i=1;i<=H;i++){
            for(int j=1;j<=W;j++){
                cin >> c;
                if(c=='X') F[i][j] = inf;
                else if(c=='S'){
                    dp[i][j][i][j][0] = 0;
                    dp[i][j][i][j][1] = 0;
                    F[i][j] = 0;
                    Q.push((state){0,i,j,i,j,true});
                    Q.push((state){0,i,j,i,j,false});
                }else if(c=='T'){
                    goal.push_back({i,j});
                    F[i][j] = 0;
                }else F[i][j] = c-'0';
            }
        }
        while(!Q.empty()){
            state s = Q.top(); Q.pop();
//            cerr << s.time << " "  << s.lx << " " << s.ly << " " << s.rx << " " << s.ry << endl;
            if(s.time>dp[s.lx][s.ly][s.rx][s.ry][s.move]) continue;
            if(s.move){
                for(int i=1;i<=5;i++){
                    for(int j=1;j<=3;j++){
                        int x = s.lx+i-3,y = s.ly+j;
                        if(!in(x,y) || abs(s.lx-x)+abs(s.ly-y)>3) continue;
                        if(dp[s.lx][s.ly][x][y][1]>s.time+F[x][y]){
                            dp[s.lx][s.ly][x][y][1] = s.time+F[x][y];
                            Q.push((state){dp[s.lx][s.ly][x][y][1],s.lx,s.ly,x,y,false});
                        }
                    }
                }
            }
            else{
                for(int i=1;i<=5;i++){
                    for(int j=1;j<=3;j++){
                        int x = s.rx+i-3,y = s.ry-j;
                        if(!in(x,y) || abs(s.rx-x)+abs(s.ry-y)>3) continue;
                        if(dp[x][y][s.rx][s.ry][0]>s.time+F[x][y]){
                            dp[x][y][s.rx][s.ry][0] = s.time+F[x][y];
                            Q.push((state){dp[x][y][s.rx][s.ry][0],x,y,s.rx,s.ry,true});
                        }
                    }
                }
            }
        }
        int ans = inf;
        for(auto x:goal){
            int a = x.first,b = x.second;
            //cerr << a << " " << b << endl;
            for(int i=1;i<=H;i++){
                for(int j=1;j<=W;j++){
                    for(int k=0;k<2;k++){
                        ans = min(ans,min(dp[a][b][i][j][k],dp[i][j][a][b][k]));
                    }
                }
            }
        }
        cout << (ans!=inf? ans:-1) << endl;
        goal.clear();
    }
}


Measured memory_bytes: 44652