#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);++i)
#define rep2(i,a,b) for (int i=(a);i<(b);++i)
#define rrep(i,n) for (int i=(n)-1;i>=0;--i)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;--i)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(_v) cout<<(#_v)<<":";for(auto(_x):(_v)){cout<<" "<<(_x);}cout<<endl;
#define printVS(vs) cout<<(#vs)<<":"<<endl;for(auto(s):(vs)){cout<<(s)<< endl;}
#define printVV(vv) cout<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;}
#define printP(p) cout<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cout<<(#vp)<<":"<<endl;for(auto(p):(vp)){cout<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cout << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cout << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1e9;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

// 15:10-

// N, E, S, W
const int dx[4] = {-1, 0,  1,  0};
const int dy[4] = { 0, 1,  0, -1};

// d[i][j][k] = ??????(i, j)????¶?k(0?????????1??????)??§??\??????????????¢

int w, h;
char a[100][100];
int d[100][100][2];

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

int dijkstra() {
    using State = tuple<int, int, int, int>; // cost, i, j, k
    priority_queue<State, vector<State>, greater<State>> pq;
    rep(i, h) rep(j, w) rep(k, 2) {
        if (a[i][j] == 'S') {
            d[i][j][k] = 0;
            pq.push(State(0, i, j, k));
        }
        else {
            d[i][j][k] = inf;
        }
    }

    while (!pq.empty()) {
        int cost, i, j, k;
        tie(cost, i, j, k) = pq.top(); pq.pop();
        if (d[i][j][k] < cost) continue;
        int nk = k^1;
        // cout << "i = " << i << ", j = " << j << ", k = " << k << ", cost = " << cost << endl;
        for (int dr = -2; dr <= +2; dr++) {
            for (int dc = 1; dc <= 3; dc++) {
                int ni = i + dr;
                int nj = j + dc * (k ? +1 : -1);
                if (!inside(ni, nj)) continue;
                if (abs(ni - i) + abs(nj - j) > 3) continue;
                if (a[ni][nj] == 'X') continue;
                int ncost = 0;
                // cout << "ni = " << ni << ", nj = " << nj << ", nk = " << nk << ", ncost = " << ncost << endl;
                if (isdigit(a[ni][nj])) ncost = a[ni][nj] - '0';
                if (d[ni][nj][nk] > cost + ncost) {
                    d[ni][nj][nk] = cost + ncost;
                    if (a[ni][nj] != 'T') pq.push(State(cost + ncost, ni, nj, nk));
                }
            }
        }
    }

    int ret = inf;
    rep(i, h) rep(j, w) rep(k, 2) {
        if (a[i][j] == 'T') chmin(ret, d[i][j][k]);
    }
    if (ret == inf) ret = -1;
    return ret;
}

main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int testcase = 0;
    while (cin >> w >> h, w) {
        // cerr << "--- testcase " << ++testcase << " ---" << endl;
        rep(i, h) rep(j, w) cin >> a[i][j];
        // if (testcase != 1) continue;
        cout << dijkstra() << endl;
    }
}

Measured memory_bytes: 3232