#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};


const double EPS = 1e-9;

struct State{
    int step;
    bool next_right;
    int y,x;
    State(int s,bool nr,int y,int x) : step(s),next_right(nr),y(y),x(x) {};
    bool operator<(const State& rhs){
        return step < rhs.step;
    }
};


struct Comp{
    bool operator()(State& left,State& right){
        return left.step > right.step;
    };
};

int main(){
    while(true){
        int w,h;
        cin >> w >> h;
        if(w == 0 and h == 0) break;
        vector<vector<char> > S(h,vector<char>(w));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> S[i][j];
            }
        }

        set<pair<int,int> > goal;
        priority_queue<State,vector<State>,Comp > que;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(S[i][j] == 'S'){
                    que.push(State(0,true,i,j));
                    que.push(State(0,false,i,j));
                }else if(S[i][j] == 'T'){
                    goal.insert(make_pair(i,j));
                }
            }
        }
        int ret = -1;
        // right?
        vector<vector<vector<char> > > used(h,vector<vector<char> >(w,vector<char>(2,false)));
        while(not que.empty()){
            State s = que.top();
            que.pop();
            if(goal.find(make_pair(s.y,s.x)) != goal.end()){
                ret = s.step;
                break;
            }
            if(used[s.y][s.x][s.next_right]) continue;
            used[s.y][s.x][s.next_right] = true;
            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){
                    if(s.next_right){
                        if(s.x < j and abs(i-s.y)+abs(j-s.x) <= 3){
                            if(S[i][j] == 'T'){
                                que.push(State(s.step,!s.next_right,i,j));
                            }else if(S[i][j] != 'S' and S[i][j] != 'X'){
                                que.push(State(s.step+S[i][j]-'0',!s.next_right,i,j));
                            }
                        }
                    }else{
                        if(s.x > j and abs(i-s.y)+abs(j-s.x) <= 3){
                            if(S[i][j] == 'T'){
                                que.push(State(s.step,!s.next_right,i,j));
                            }else if(S[i][j] != 'S' and S[i][j] != 'X'){
                                que.push(State(s.step+S[i][j]-'0',!s.next_right,i,j));
                            }
                        }
                    }
                }
            }
        }
        cout << ret << endl;
    }
    return 0;
}

Measured memory_bytes: 1356