//
//  main.cpp
//
// g++ -std=c++14 main.cpp
//

//#include "main.hpp"
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <tuple>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll inf = 1e9, d[4000], w, h;
vector<pair<ll, ll> > G[4000];
// goto, cost

void dijkstra(ll s) {
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d + 2 * w * h, inf);
    d[s] = 0;
    que.push(P(0, s));
    
    while (!que.empty()) {
        P p = que.top();  que.pop();
        ll v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            P e = G[v][i];
            if (d[e.first] > d[v] + e.second) {
                d[e.first] = d[v] + e.second;
                que.push(P(d[e.first], e.first));
            }
        }
    }
}

int main(void){
    while (cin >> w >> h) {
        for (int i = 0; i < 4000; i++) G[i].clear();
        
        if (w * h == 0) return 0;
        
        vector<int> start, goal;
        int s[100][100] = {};
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                char c[1];
                cin >> c;
                if (c[0] == 'S') {
                    start.push_back(i * w + j);
                    s[i][j] = 0;
                }
                else if (c[0] == 'T') {
                    goal.push_back(i * w + j);
                    s[i][j] = 0;
                }
                else if (c[0] == 'X') s[i][j] = inf;
                else s[i][j] = int(c[0] - '0');
            }
        }
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                // left: + 0, right: + w * h
                for (int p = i - 3; p <= i + 3; p++) {
                    for (int q = j - 3; q <= j + 3; q++) {
                        if (p < 0 || p >= h || q < 0 || q >= w) ;
                        else if (q < j && abs(p - i) + abs(q - j) <= 3) {
                            // right -> left
                            if (s[p][q] != inf)
                            G[i * w + j + w * h].emplace_back(p * w + q, s[p][q]);
                        }
                        else if (q > j && abs(p - i) + abs(q - j) <= 3) {
                            // left -> right
                            if (s[p][q] != inf)
                            G[i * w + j].emplace_back(p * w + q + w * h, s[p][q]);
                        }
                    }
                }
            }
        }
        
        /*
        for (int i = 0; i <  w * h * 2; i++) {
            cout << i << " " ;
            for (int j = 0; j < G[i].size(); j++) cout << G[i][j].first << " " << G[i][j].second << " ";
            cout << endl;
        }*/
        
        ll ans = inf;
        for (int i = 0; i < start.size(); i++) {
            dijkstra(start[i]);
            for (int j = 0; j < goal.size(); j++) {
                ans = min(ans, d[goal[j]]);
                ans = min(ans, d[goal[j] + w * h]);
            }
            dijkstra(start[i] + w * h);
            for (int j = 0; j < goal.size(); j++) {
                ans = min(ans, d[goal[j]]);
                ans = min(ans, d[goal[j] + w * h]);
            }
        }
        
        if (ans == inf) ans = -1;
        cout << ans << endl;
    }
}

/*
 g++ -std=c++14 main.cpp
*/



Measured memory_bytes: 3904