#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define len(v) (int)(v).size()
#define show(x) cout << #x << " = " << (x) << "\n"
#define INF 1000000007
#define fi first
#define se second

using namespace std;

typedef pair<int,int>P;
typedef pair<P,int>pi;

int dx[9] = {2,1,1,0,0,0,-1,-1,-2};
int dy[9] = {1,1,2,1,2,3,1,2,1};
int dp[61][31][2];

int main()
{
    while(1){
        int w,h;
        cin >> w >> h;
        if(w == 0){
            break;
        }
        vector<vector<string> > s(h,vector<string>(w));
        vector<P> st;
        vector<P> goal;
        rep(i,h){
            rep(j,w){
                cin >> s[i][j];
                if(s[i][j] == "S"){
                    st.push_back(P(i,j));
                }else if(s[i][j] == "T"){
                    goal.push_back(P(i,j));
                }
            }
        }
        int ans = INF;
        rep(i,len(st)){
            rep(j,h){
                rep(k,w){
                    rep(l,2){
                        dp[j][k][l] = INF;
                    }
                }
            }
            queue<pi> que;
            que.push(pi(st[i],-1));
            dp[st[i].fi][st[i].se][0] = 0;
            while(!que.empty()){
                pi pp = que.front();
                P p = pp.fi;
                int cri = pp.se;
                int x = (cri+1)/2;
                int y = (x^1);
                que.pop();
                rep(k,9){
                    int nx = p.fi+dx[k], ny = p.se+cri*dy[k];
                    if(nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] != "X" && dp[nx][ny][y] > dp[p.fi][p.se][x]+(isdigit(s[nx][ny][0])?(s[nx][ny][0]-'0'):0)){
                        dp[nx][ny][y] = dp[p.fi][p.se][x]+(isdigit(s[nx][ny][0])?(s[nx][ny][0]-'0'):0);
                        que.push(pi(P(nx,ny),-cri));
                    }
                }
            }
            rep(k,len(goal)){
                ans = min(ans,min(dp[goal[k].fi][goal[k].se][0],dp[goal[k].fi][goal[k].se][1]));
            }
            rep(j,h){
                rep(k,w){
                    rep(l,2){
                        dp[j][k][l] = INF;
                    }
                }
            }
            que.push(pi(st[i],1));
            dp[st[i].fi][st[i].se][1] = 0;
            while(!que.empty()){
                pi pp = que.front();
                P p = pp.fi;
                int cri = pp.se;
                int x = (cri+1)/2;
                int y = (x^1);
                que.pop();
                rep(k,9){
                    int nx = p.fi+dx[k], ny = p.se+cri*dy[k];
                    if(nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] != "X" && dp[nx][ny][y] > dp[p.fi][p.se][x]+(isdigit(s[nx][ny][0])?(s[nx][ny][0]-'0'):0)){
                        dp[nx][ny][y] = dp[p.fi][p.se][x]+(isdigit(s[nx][ny][0])?(s[nx][ny][0]-'0'):0);
                        que.push(pi(P(nx,ny),-cri));
                    }
                }
            }
            rep(k,len(goal)){
                ans = min(ans,min(dp[goal[k].fi][goal[k].se][0],dp[goal[k].fi][goal[k].se][1]));
            }
        }
        // rep(i,h){
        //     rep(j,w){
        //         cout << min(dp[i][j][0],dp[i][j][1]) << " ";
        //     }
        //     cout << "\n";
        // }
        if(ans == INF){
            cout << "-1\n";
        }else{
            cout << ans << "\n";
        }
    }
}



Measured memory_bytes: 3172