#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

#define INF 180000

int t[60][30];
int ml[60][30];
int mr[60][30];

int main() {
    while (1) {
        int i;
        int w, h;
        vector<pair<int, int> > goal;
        queue<pair<pair<int, int>, bool> > process;
        int ldx[] = {0,  0, -1, 0};
        int ldy[] = {-1, 0, 0, 1};
        int rdx[] = {0,  0, 1, 0};
        int rdy[] = {-1, 0, 0, 1};

        int min = INF;

        scanf("%d %d", &w, &h);
        if (w == 0 && h == 0) return 0;

        for (i=0;i<h;i++) {
            int j;
            for (j=0;j<w;j++) {
                ml[i][j] = INF;
                mr[i][j] = INF;
            }
        }

        for (i=0;i<h;i++) {
            int j;
            for (j=0;j<w;j++) {
                char c;
                scanf(" %c", &c);

                if (c == 'S') {
                    t[i][j] = 0;
                    ml[i][j] = 0;
                    mr[i][j] = 0;
                    process.push(make_pair(make_pair(j, i), 0));
                    process.push(make_pair(make_pair(j, i), 1));
                } else if (c == 'X') {
                    t[i][j] = INF;
                } else if (c == 'T') {
                    t[i][j] = 0;
                    goal.push_back(make_pair(j, i));
                } else {
                    t[i][j] = c - 48;
                }
            }
        }

        while (!process.empty()) {
            int x1, y1;
            bool is_right;
            pair<pair<int, int>, bool>  point = process.front();

            x1 = point.first.first;
            y1 = point.first.second;
            is_right = point.second;

            for (i=0;i<4;i++) {
                int j;
                for (j=0;j<4;j++) {
                    int value;
                    int x2, y2;

                    if (is_right) {
                        x2 = x1 + rdx[i] + rdx[j] + 1;
                        y2 = y1 + rdy[i] + rdy[j];
                        if (x2 < 0 || x2 >= w || y2 < 0 || y2 >= h) continue;
                        value = ml[y1][x1] + t[y2][x2];

                        if (mr[y2][x2] > value) {
                            mr[y2][x2] = value;
                            process.push(make_pair(make_pair(x2, y2), 0));
                        }
                    } else {
                        x2 = x1 + ldx[i] + ldx[j] - 1;
                        y2 = y1 + ldy[i] + ldy[j];
                        if (x2 < 0 || x2 >= w || y2 < 0 || y2 >= h) continue;
                        value = mr[y1][x1] + t[y2][x2];

                        if (ml[y2][x2] > value) {
                            ml[y2][x2] = value;
                            process.push(make_pair(make_pair(x2, y2), 1));
                        }
                    }
                }
            }

            process.pop();
        }

        vector<pair<int, int> >::iterator it = goal.begin();
        while (it != goal.end()) {
            int x, y;

            x = it->first;
            y = it->second;
            if (min > ml[y][x]) min = ml[y][x];
            if (min > mr[y][x]) min = mr[y][x];
            it++;
        }

        if (min == INF) min = -1;

        printf("%d\n", min);
    }
}

Measured memory_bytes: 1088