#include <cstdio>
#include <cctype>
#include <queue>
using namespace std;

int W, H;
char board[100][100];
int dist[100][100][2];
const int INF = 1 << 28;

const int LEFT  = 0;
const int RIGHT = 1;
const int dx[][9] = {{0, 0, 0, 1, 1, 2, -1, -1, -2}, {0, 0, 0, 1, 1, 2, -1, -1, -2}};
const int dy[][9] = {{1, 2, 3, 1, 2, 1, 1, 2, 1}, {-1, -2, -3, -1, -2, -1, -1, -2, -1}};

struct Elem {
    int x, y, foot, dist;
    bool operator<(const Elem &e) const {
        return dist > e.dist;
    }
};

void solve() {
    priority_queue<Elem> que;
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            if(board[i][j] == 'S') {
                que.push(Elem{i, j, 0, 0});
                que.push(Elem{i, j, 1, 0});
                dist[i][j][0] = dist[i][j][1] = 0;
            }
            else {
                dist[i][j][0] = dist[i][j][1] = INF;
            }
        }
    }

    int ans = INF;
    while(que.size()) {
        Elem cur = que.top(); que.pop();
        int f = cur.foot;
        for(int k=0; k<9; k++) {
            int nx = cur.x + dx[f][k];
            int ny = cur.y + dy[f][k];
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(board[nx][ny] == 'X') continue;

            bool t_or_s = !isdigit(board[nx][ny]);
            int cost = (t_or_s ? 0 : board[nx][ny] - '0');
            if(dist[nx][ny][f^1] > dist[cur.x][cur.y][f] + cost) {
                dist[nx][ny][f^1] = dist[cur.x][cur.y][f] + cost;
                que.push(Elem{nx, ny, f^1, dist[nx][ny][f^1]});
                if(board[nx][ny] == 'T') {
                    if(ans > dist[nx][ny][f^1]) {
                        ans = dist[nx][ny][f^1];
                    }
                }
            }
        }
    }

    printf("%d\n", (ans == INF ? -1 : ans));
}

int main() {
    while(1) {
        scanf("%d%d", &W, &H);
        if(W == 0 && H == 0) break;

        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++) {
                scanf(" %c", &board[i][j]);
            }
        }

        solve();
    }
    return 0;
}
