#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
#define MAXH 64
#define MAXW 32

typedef long long ll;
const int dx[] = {1, 1, 1, 1, 1, 2, 2, 2, 3};
const int dy[] = {-2, -1, 0, 1, 2, -1, 0, 1, 0};

struct edge {
    int x, y, cost;
};

struct state {
    int x, y, all_cost, lr;
};

int w, h;
char field[MAXH][MAXW];

int dijkstra(void) {
    int d[MAXH][MAXW][2];
    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;
    state s;
    queue<state> que;
    s.y = h-1; s.all_cost = 0;
    for (int i = 0; i < w; i++) {
        if (field[h-1][i] == 'S') {
            d[h-1][i][0] = d[h-1][i][1] = 0;
            s.x = i; s.lr = 0; que.push(s); s.lr = 1; que.push(s);
        }
    }
    while (!que.empty()) {
        state now = que.front(); que.pop();
        if (d[now.y][now.x][now.lr] < now.all_cost || field[now.y][now.x] == 'T') continue;
        if (now.lr == 0) { // 左の場合
            for (int i = 0; i < 9; i++) {
                int nx = now.x + dx[i];
                int ny = now.y + dy[i];
                if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] != 'X') {
                    int all_cost;
                    if (field[ny][nx] == 'T' || field[ny][nx] == 'S') {
                        all_cost = now.all_cost;
                    } else {
                        all_cost = now.all_cost + (int)(field[ny][nx] - '0');
                    }
                    if (d[ny][nx][1] > all_cost) {
                        d[ny][nx][1] = all_cost;
                        state st; st.x = nx; st.y = ny; st.all_cost = all_cost; st.lr = 1;
                        que.push(st);
                    }
                }
            }
        }
        else { // 右の場合
            for (int i = 0; i < 9; i++) {
                int nx = now.x - dx[i];
                int ny = now.y + dy[i];
                if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] != 'X') {
                    int all_cost;
                    if (field[ny][nx] == 'T' || field[ny][nx] == 'S') {
                        all_cost = now.all_cost;
                    } else {
                        all_cost = now.all_cost + (int)(field[ny][nx] - '0');
                    }
                    if (d[ny][nx][0] > all_cost) {
                        d[ny][nx][0] = all_cost;
                        state st; st.x = nx; st.y = ny; st.all_cost = all_cost; st.lr = 0;
                        que.push(st);
                    }
                }
            }
        }
    }
    int ret = INF;
    for (int i = 0; i < w; i++) {
        if (field[0][i] == 'T') {
            for (int j = 0; j < 2; j++) {
                ret = min(ret, d[0][i][j]);
            }
        }
    }
    if (ret < INF) return ret;
    else return -1;
}

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

Measured memory_bytes: 1256