#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
	while(true) {
		ll w, h;
		cin>>w>>h;
		if(w == 0 && h == 0) break;
		vector<vector<char> > cell(h, vector<char>(w));
		vector<P> s;
		vector<P> g;
		REP(i, h) {
			REP(j, w) {
				cin>>cell[i][j];
				if(cell[i][j] == 'S') s.pb(P(i, j));
				if(cell[i][j] == 'T') g.pb(P(i, j));
			}
		}
		vector<vector<vector<ll> > > d(h, vector<vector<ll> >(w, vector<ll>(2, INF)));
		priority_queue<pair<ll, pair<P, ll> >, vector<pair<ll, pair<P, ll> > > , greater<pair<ll, pair<P, ll> > > > q;
		while(s.size()) {
			REP(i, 2) {
				d[s[s.size() - 1].first][s[s.size() - 1].second][i] = 0;
				q.push({0, {P(s[s.size() - 1].first, s[s.size() - 1].second), i}});
				while(!q.empty()) {
					pair<ll, pair<P, ll> > p = q.top();
					q.pop();
					if(d[p.second.first.first][p.second.first.second][p.second.second] != p.first) continue;
					switch(p.second.second) {
						case 0: {
							FOR(j, 1, 4) {
								FOR(k, (- 3 + j), (3 - j) + 1) {
									ll nh = p.second.first.first + k;
									ll nw = p.second.first.second + j;
									if(nh >= 0 && nh < h && nw >= 0 && nw < w && cell[nh][nw] != 'X') {
										if(cell[nh][nw] == 'T') {
											if(d[nh][nw][1] > d[p.second.first.first][p.second.first.second][0]) {
											d[nh][nw][1] = d[p.second.first.first][p.second.first.second][0];
											q.push({d[nh][nw][1], {P(nh, nw), 1}});
											}
											continue;
										}
										if(cell[nh][nw] == 'S') continue;
										if(d[nh][nw][1] > d[p.second.first.first][p.second.first.second][0] + (cell[nh][nw] - '0')) {
											d[nh][nw][1] = d[p.second.first.first][p.second.first.second][0] + (cell[nh][nw] - '0');
											q.push({d[nh][nw][1], {P(nh, nw), 1}});
										}
									}
								}
							}
							break;
						}
						case 1: {
							FOR(j, -3, 0) {
								FOR(k, (- 3 - j), (3 + j) + 1) {
									ll nh = p.second.first.first + k;
									ll nw = p.second.first.second + j;
									if(nh >= 0 && nh < h && nw >= 0 && nw < w && cell[nh][nw] != 'X') {
										if(cell[nh][nw] == 'T') {
											if(d[nh][nw][0] > d[p.second.first.first][p.second.first.second][1]) {
											d[nh][nw][0] = d[p.second.first.first][p.second.first.second][1];
											q.push({d[nh][nw][0], {P(nh, nw), 0}});
											}
											continue;
										}
										if(cell[nh][nw] == 'S') continue;
										if(d[nh][nw][0] > d[p.second.first.first][p.second.first.second][1] + (cell[nh][nw] - '0')) {
											d[nh][nw][0] = d[p.second.first.first][p.second.first.second][1] + (cell[nh][nw] - '0');
											q.push({d[nh][nw][0], {P(nh, nw), 0}});
										}
									}
								}
							}
							break;
						}
					}
				}
			}
			s.pop_back();
		}
		ll ans = INF;
		while(g.size()) {
			REP(i, 2) {
				ans = min(ans, d[g[g.size() - 1].first][g[g.size() - 1].second][i]);
			}
			g.pop_back();
		}
		if(ans != INF) cout<<ans<<endl;
		else cout<<-1<<endl;
	}
}

Measured memory_bytes: 3220