#include <bits/stdc++.h>
using namespace std;

int INF = 1'000'000'000;
typedef tuple<double, int, int> T;

int main(){
    while (1){
        int w, h;
        cin >> w >> h;
        if (w == 0) break;
        priority_queue<T, vector<T>, greater<T>> que_left;
        priority_queue<T, vector<T>, greater<T>> que_right;
        vector<vector<char>> clif(h, vector<char>(w));
        int t = 0;
        int left = 0;
        int right = 1;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin >> clif[i][j];
                if (clif[i][j] == 'S'){
                    int pos = i * 100 + j;
                    que_left.push(tie(t, left, pos));
                    que_right.push(tie(t, right, pos));
                }
            }
        }
        vector<vector<bool>> check_left(h, vector<bool>(w, false));
        vector<vector<bool>> check_right(h, vector<bool>(w, false));

        int ans = INF;
        while(que_left.size()){
            int d = get<0>(que_left.top());
            int mode = get<1>(que_left.top());
            int x = get<2>(que_left.top());
            int y = x % 100; x /= 100;
            que_left.pop();
            if (mode == 0 && check_left[x][y]) continue;
            if (mode == 1 && check_right[x][y]) continue;
            if (clif[x][y] == 'T'){
                ans = d;
                break;
            }
            if (mode == 0){
                check_left[x][y] = true;
                for (int i = -2; i < 3; i++){
                    for (int j = 1; j <= 3; j++){
                        if ((abs(i) + j) > 3) continue;
                        if (0 <= x + i && x + i < h && 0 <= y + j && y + j < w && clif[x+i][y+j] != 'X'){
                            int nmode = 1;
                            int nx = x + i;
                            int ny = y + j;
                            int nd = d;
                            if (clif[nx][ny] != 'S' && clif[nx][ny] != 'T') nd += (clif[nx][ny] - '0');
                            nx *= 100;
                            nx += ny;
                            que_left.push(tie(nd, nmode, nx));
                        }
                    }
                }
            }
            else {
                check_right[x][y] = true;
                for (int i = -2; i < 3; i++){
                    for (int j = -3; j < 0; j++){
                        if (abs(i) + abs(j) > 3) continue;
                        if (0 <= x + i && x + i < h && 0 <= y + j && y + j < w && clif[x+i][y+j] != 'X'){
                            int nmode = 0;
                            int nx = x + i;
                            int ny = y + j;
                            int nd = d;
                            if (clif[nx][ny] != 'S' && clif[nx][ny] != 'T') nd += (clif[nx][ny] - '0');
                            nx *= 100;
                            nx += ny;
                            que_left.push(tie(nd, nmode, nx));
                        }
                    }
                }
            }
        }
        
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                check_left[i][j] = false;
                check_right[i][j] = false;
            }
        }

        while(que_right.size()){
            int d = get<0>(que_right.top());
            int mode = get<1>(que_right.top());
            int x = get<2>(que_right.top());
            int y = x % 100; x /= 100;
            que_right.pop();
            if (mode == 0 && check_left[x][y]) continue;
            if (mode == 1 && check_right[x][y]) continue;
            if (clif[x][y] == 'T'){
                ans = min(ans, d);
                break;
            }
            if (mode == 0){
                check_left[x][y] = true;
                for (int i = -2; i < 3; i++){
                    for (int j = 1; j <= 3; j++){
                        if ((abs(i) + j) > 3) continue;
                        if (0 <= x + i && x + i < h && 0 <= y + j && y + j < w && clif[x+i][y+j] != 'X'){
                            int nmode = 1;
                            int nx = x + i;
                            int ny = y + j;
                            int nd = d;
                            if (clif[nx][ny] != 'S' && clif[nx][ny] != 'T') nd += (clif[nx][ny] - '0');
                            nx *= 100;
                            nx += ny;
                            que_right.push(tie(nd, nmode, nx));
                        }
                    }
                }
            }
            else {
                check_right[x][y] = true;
                for (int i = -2; i < 3; i++){
                    for (int j = -3; j < 0; j++){
                        if (abs(i) + abs(j) > 3) continue;
                        if (0 <= x + i && x + i < h && 0 <= y + j && y + j < w && clif[x+i][y+j] != 'X'){
                            int nmode = 0;
                            int nx = x + i;
                            int ny = y + j;
                            int nd = d;
                            if (clif[nx][ny] != 'S' && clif[nx][ny] != 'T') nd += (clif[nx][ny] - '0');
                            nx *= 100;
                            nx += ny;
                            que_right.push(tie(nd, nmode, nx));
                        }
                    }
                }
            }
        }

        if (ans != INF) cout << ans << endl;
        else cout << -1 << endl;
    }
}


Measured memory_bytes: 3168