#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


/*
ll dfs(vector<string> &v, vector<vvl> &dp, int i, int j, int t) {
	if (dp[i][j][t] != -1)return dp[i][j][t];
	if (v[i][j] == 'T')return dp[i][j][t] = 0;
	dp[i][j][t] = INF;
	ll ret = INF;
	if (t == 0) {
		for (int y = j + 1; y <= j + 3; ++y) {
			for (int x = i - (2 - (y - j)); x <= i + (2 - (y - j)); ++x) {
				if (x >= h || x < 0 || y >= w || y < 0 || v[x][y] == 'X')continue;
				int cost;
				if (isdigit(v[x][y]))cost = v[x][y] - '0';
				else cost = 0;
				ret = min(ret, dfs(v, dp, x, y, 1) + cost);
			}
		}
	}
	else {
		for (int y = j - 3; y <= j - 1; ++y) {
			for (int x = i - (2 - (j - y)); x <= i + (2 - (j - y)); ++x) {
				if (x >= h || x < 0 || y >= w || y < 0 || v[x][y] == 'X')continue;
				int cost;
				if (isdigit(v[x][y]))cost = v[x][y] - '0';
				else cost = 0;
				ret = min(ret, dfs(v, dp, x, y, 0) + cost);
			}
		}
	}
	return dp[i][j][t] = ret;
}
*/
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int w, h;
	while (cin >> w >> h, w) {
		vector<string> v(h);
		REP(i, h) {
			string s;
			REP(j, w) {
				char c;
				cin >> c;
				s += c;
			}
			v[i] = s;
		}
		vector<vvi> dp(h, vvi(w, vi(2, INF)));

		priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<>> q;
		REP(i, h)REP(j, w) if (v[i][j] == 'S') {
			dp[i][j][0] = 0;
			dp[i][j][1] = 0;
			q.push({ 0,{i*w + j,0} });
			q.push({ 0,{i*w + j,1} });
		}
		while (!q.empty()) {
			auto p = q.top(); q.pop();
			int d = p.first;
			int i = p.second.first / w, j = p.second.first%w;
			int t = p.second.second;
			if (v[i][j] == 'T')continue;
			if (t == 0) {
				for (int y = j + 1; y <= j + 3; ++y) {
					for (int x = i - (3 - (y - j)); x <= i + (3 - (y - j)); ++x) {
						if (x >= h || x < 0 || y >= w || y < 0 || v[x][y] == 'X')continue;
						int cost;
						if (isdigit(v[x][y]))cost = v[x][y] - '0';
						else cost = 0;
						if (cost + d < dp[x][y][1]) {
							dp[x][y][1] = cost + d;
							q.push({ cost + d,{x*w+y,1} });
						}
					}
				}
			}
			else {
				for (int y = j - 3; y <= j - 1; ++y) {
					for (int x = i - (3 - (j - y)); x <= i + (3 - (j - y)); ++x) {
						if (x >= h || x < 0 || y >= w || y < 0 || v[x][y] == 'X')continue;
						int cost;
						if (isdigit(v[x][y]))cost = v[x][y] - '0';
						else cost = 0;
						if (cost + d < dp[x][y][0]) {
							dp[x][y][0] = cost + d;
							q.push({ cost + d,{x*w+y,0} });
						}
					}
				}
			}
		}
		int ans = INF;
		REP(i, h)REP(j, w)if (v[i][j] == 'T')ans = min(ans, min(dp[i][j][0], dp[i][j][1]));
		if (ans == INF)cout << -1 << endl;
		else cout << ans << endl;
	}

}


Measured memory_bytes: 3240