#include <iostream>
#include <algorithm>
using namespace std;

#define H 60
#define W 30
#define INF 10000
#define R 1
#define L 0
int w, h;
char maps[H][W];
struct point{
    int x, y;
};

bool invalid(int x, int y){
    return (x < 0 || x >= w || y < 0 || y >= h);
}

int dijkstra(){
    int d[H][W][2];
    bool used[H][W][2];
    int ldx[9] = {-1, -1, -1, -1, -1, -2, -2, -2, -3};
    int ldy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
    int rdx[9] = {1, 1, 1, 1, 1, 2, 2, 2, 3};
    int rdy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
    
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int k = 0; k < 2; k++) {
                d[i][j][k] = INF;
                used[i][j][k] = false;
            }
        }
    }
    for (int i = 0; i < w; i++) {
        if (maps[h-1][i] == 'S') {
            d[h - 1][i][L] = 0; d[h - 1][i][R] = 0;
        }
    }
    
    while (true) {
        point p;
        p.x = -1; p.y = -1;
        int muki = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                for (int k = 0; k < 2; k++) {
                    if (!used[i][j][k] && (p.x == -1 || d[i][j][k] < d[p.y][p.x][muki])) {
                        p.x = j; p.y = i; muki = k;
                    }
                }
            }
        }
        
        if (p.x == -1 && p.y == -1) {
            break;
        }
        used[p.y][p.x][muki] = true;
        
        if (muki == R) {
            for (int i = 0; i < 9; i++) {
                int mx = p.x + ldx[i];
                int my = p.y + ldy[i];
                
                if (invalid(mx, my)) {
                    continue;
                }
                if (maps[my][mx] == 'X') {
                    continue;
                }
                if (maps[my][mx] == 'S' || maps[my][mx] == 'T') {
                    d[my][mx][L] = min(d[my][mx][L], d[p.y][p.x][R]);
                }
                else{
                    d[my][mx][L] = min(d[my][mx][L], d[p.y][p.x][R] + (maps[my][mx] - '0'));
                }
            }
        }
        else{
            for (int i = 0; i < 9; i++) {
                int mx = p.x + rdx[i];
                int my = p.y + rdy[i];
                
                if (invalid(mx, my)) {
                    continue;
                }
                if (maps[my][mx] == 'X') {
                    continue;
                }
                if (maps[my][mx] == 'S' || maps[my][mx] == 'T') {
                    d[my][mx][R] = min(d[my][mx][R], d[p.y][p.x][L]);
                }
                else{
                    d[my][mx][R] = min(d[my][mx][R], d[p.y][p.x][L] + (maps[my][mx] - '0'));
                }
            }
        }
    }
    
    int ans = INF;
    for (int j = 0; j < w; j++) {
        if (maps[0][j] == 'T'){
            for (int k = 0; k < 2; k++) {
                ans = min(ans, d[0][j][k]);
            }
        }
    }
    if (ans == INF) {
        return -1;
    }
    else {
        return ans;
    }
}

int main()
{
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> maps[i][j];
            }
        }
        cout << dijkstra() << endl;
    }
    return 0;
}

Measured memory_bytes: 1176