// start: 21:57

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

#define REP(i, n) for ( int i = 0; i < n; i++ )

using namespace std;

typedef pair<int, int> xy;

// テ・ツキツヲティツカツウテ」ツ?ョテ」ツ?ィテ」ツ?催」ツ?ォテァツスツョテ」ツ?妥」ツつ凝・ツ渉ウティツカツウ
int mx[] = {1, 1, 2, 1, 2, 3, 1, 2, 1},
    my[] = {2, 1, 1, 0, 0, 0, -1, -1, -2};

void dump(vector< vector<char> > &f, int x = -1, int y = -1, int l = -1) {
    REP(i, f.size()) {
        REP(j, f[i].size()) {
            if ( i == y && j == x ) {
                if ( l == 0 )
                    cout << "R";
                else
                    cout << "L";
            }
            else
                cout << f[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

int main() {
    int w, h;

    while(cin >> w >> h, w||h) {
        int closed[h][w][2];
        vector< queue<xy> > foot(2);   // 0:テ・ツ渉ウ 1:テ・ツキツヲ
        queue<int> cost, leg;
        vector< vector<char> > f(h);
        vector< xy > start;

        REP(i, h) REP(j, w) REP(k, 2) closed[i][j][k] = 0;

        REP(i, h) {
            REP(j, w) {
                char tmp;
                cin >> tmp;
                f[i].push_back(tmp);
                if ( f[i][j] == 'S' ) {
                    start.push_back( make_pair(j, i) );
                }
            }
        }

        // テ」ツつケテ」ツつソテ」ツδシテ」ツδ暗・ツ慊ーテァツつケテッツシツ凝・ツキツヲテ・ツ渉ウテ」ツつ津・ツ按敕ヲツ慊淌・ツ?、テ」ツ?ィテ」ツ?療」ツ?ヲpush
        REP(i, start.size()) {
            int l = 0;
            leg.push(l);
            foot[l].push( make_pair(start[i].first, start[i].second) );
            cost.push(0);

            l = 1;
            leg.push(l);
            foot[l].push( make_pair(start[i].first, start[i].second) );
            cost.push(0);

            // cout << start[i].first << " " << start[i].second << " " << f[start[i].second][start[i].first] << endl;
        }

        int min = INT_MAX;
        while(!cost.empty()) {
            int c = cost.front(); cost.pop();
            int l = leg.front(); leg.pop();
            xy ft = foot[l].front(); foot[l].pop();
            int x = ft.first, y = ft.second;

            // printf("f:%d, x:%d, y:%d, xy:%c\n", l, x, y, f[y][x]);
            // dump(f, x, y, l);
            if ( f[y][x] == 'T' && c < min ) {
                min = c;
                continue;
            }

            REP(i, 9) {
                int dir = l == 0 ? -1: 1,
                    nx = x + mx[i] * dir,
                    ny = y + my[i],
                    nl = !l;

                if ( !(0 <= nx && nx < w && 0 <= ny && ny < h) ) continue;
                if ( f[ny][nx] == 'X' || f[ny][nx] == 'S' ) continue;   // テ」ツδ静」ツつーテ・ツ?コテ」ツつ凝」ツ?凝」ツつ?

                // int nc = (int)f[ny][nx] - 48;
                int nc;
                if ( ((int)f[ny][nx] - 48) > 9 )
                    nc = c + 1;
                else
                    nc = c + ((int)f[ny][nx] - 48);
                // nc += c;
                // int nc = c + 1;
                // cout << nc << " " << f[ny][nx] << endl;

                if ( closed[ny][nx][nl] != 0 && closed[ny][nx][nl] < nc ) continue;

                cost.push(nc);
                leg.push(nl);   // テゥツ??ィツカツウ
                foot[nl].push( make_pair(nx, ny) );
                closed[ny][nx][nl] = nc;

            }
            // cout << c << endl;
            // REP(i, h) {REP(j, w) {cout << closed[i][j][0];} cout << endl;}
            // cout << endl << c << endl;
            // REP(i, h) {REP(j, w) {cout << closed[i][j][1];} cout << endl;}
            // cout << endl;

        }

        if ( min == INT_MAX )
            cout << -1 << endl;
        else
            cout << (min-1) << endl;
    }
    return 0;
}

Measured memory_bytes: 1824