#include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}

struct Edge {
    int to;
    ll cost;
};

ll cost(char c) {
    if (c == 'T' || c == 'S') {
        return 0;
    } else if (c == 'X') {
        return 1LL << 60;
    } else {
        return c - '0';
    }
}

void solve() {
    int W, H;
    while(cin >> W >> H, W) {
        vector<vector<char>> A(H, vector<char>(W));
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> A[i][j];
            }
        }

        vector<vector<Edge>> G(2 * H * W);
        for(int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                for(int k = -3; k <= 3; k++) {
                    for(int l = -3; l <= 3; l++) {
                        int lx = j, ly = i;
                        int rx = j, ry = i;
                        int nx = j + l, ny = i + k;
                        if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
                        if (lx < nx && abs(lx - nx) + abs(ly - ny) <= 3) {
                            G[ly * W + lx].push_back({W * H + ny * W + nx, cost(A[ny][nx])});
                        }
                        if (nx < rx && abs(rx - nx) + abs(ry - ny) <= 3) {
                            G[W * H + ry * W + rx].push_back({ny * W + nx, cost(A[ny][nx])});
                        }
                    }
                }
            }
        }

        priority_queue<pii, vector<pii>, greater<pii>> Q;
        for(int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (A[i][j] == 'S') {
                    Q.push({0, i * W + j});
                    Q.push({0, W * H + i * W + j});
                }
            }
        }

        vector<ll> dist(2 * W * H, 1LL << 60);
        while(!Q.empty()) {
            pii p = Q.top(); Q.pop();
            ll d = p.first;
            if (d >= dist[p.second]) continue;
            dist[p.second] = d;
            for(int i = 0; i < G[p.second].size(); i++) {
                int u = G[p.second][i].to;
                ll co = G[p.second][i].cost;
                if (dist[u] > d + co) {
                    Q.push({d + co, u});
                }
            }
        }


        ll ans = 1LL << 60;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (A[i][j] == 'T') {
                    ans = min(ans, dist[i * W + j]);
                    ans = min(ans, dist[W * H + i * W + j]);
                }
            }
        }

        if (ans == 1LL << 60) {
            cout << -1 << endl;
        } else {
            cout << ans << endl;
        }

    }
}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();

    #ifdef LOCAL_ENV
    assert(cin.rdstate() & ios_base::eofbit);
    #endif
}


Measured memory_bytes: 4060