#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define sz(c) ((int)c.size())
#define all(c) c.begin(), c.end()

using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

const int INF = 1e9;
template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;

int DY[2][9] = {
    {-2, -1, -1, 0, 0, 0, 1, 1, 2},
    {-2, -1, -1, 0, 0, 0, 1, 1, 2},
};
int DX[2][9] = {
    {1, 1, 2, 1, 2, 3, 1, 2, 1},
    {-1, -1, -2, -1, -2, -3, -1, -2, -1},
};

bool inrange(int x, int l, int r) {
    return l <= x and x < r;
}

void solve(int W, int H) {
    vvi A(H, vi(W));
    rep(i, 0, H) rep(j, 0, W) {
        char x;
        cin >> x;
        A[i][j] = x;
    }

    vector<vvi> dist(H, vvi(W, vi(2, INF)));
    using dijkstra_state = tuple<int, int, int, int>;
    heap<dijkstra_state> hp;
    rep(i, 0, H) rep(j, 0, W) if (A[i][j] == 'S') {
        dist[i][j][0] = dist[i][j][1] = 0;
        hp.push(dijkstra_state(0, i, j, 0));
        hp.push(dijkstra_state(0, i, j, 1));
    }
    while (sz(hp)) {
        int c, y, x, foot;
        tie(c, y, x, foot) = hp.top(); hp.pop();
        if (c > dist[y][x][foot]) continue;
        rep(step, 0, 9) {
            int yy = y + DY[foot][step];
            int xx = x + DX[foot][step];
            if (!(inrange(yy, 0, H) && inrange(xx, 0, W))) continue;
            if (A[yy][xx] == 'X') continue;

            int cost = A[yy][xx] - '0';
            if (A[yy][xx] == 'S' || A[yy][xx] == 'T') cost = 0;
            if (chmin(dist[yy][xx][foot ^ 1], c + cost)) {
                hp.push(dijkstra_state(dist[yy][xx][foot ^ 1], yy, xx, foot ^ 1));
            }
        }
    }

    int ans = INF;
    rep(i, 0, H) rep(j, 0, W) if (A[i][j] == 'T') {
        chmin(ans, dist[i][j][0]);
        chmin(ans, dist[i][j][1]);
    }
    if (ans == INF) ans = -1;
    cout << ans << endl;
}

signed main() {
    while (1) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h ==0 ) break;
        solve(w, h);
    }
}
