#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>


template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

int H, W;
int c(int h, int w, int isr) {
	return isr * H * W + h * W + w;
}

tuple<int, int, int> f(int X) {
	int isr = X / (H * W);
	int h = (X % (H * W)) / W;
	int w = X % W;
	return make_tuple(h, w, isr);
}

vector<ll> dijkstra(int start, vector<vector<pair<int, ll>>>& graph) {
	vector<ll> dist(graph.size(), INF); dist[start] = 0;
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; pq.emplace(0, start);
	while (!pq.empty()) {
		ll cost; int idx; tie(cost, idx) = pq.top(); pq.pop();
		if (dist[idx] < cost) continue;
		for (auto next : graph[idx]) if (chmin(dist[next.first], cost + next.second)) pq.emplace(dist[next.first], next.first);
	}
	return dist;
}

void solve() {
	cin >> W >> H; if (W == 0 and H == 0) exit(0);
	vector<vector<char>> a(H, vector<char>(W));
	for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> a[i][j];
	vector<vector<pair<int, ll>>> g(H * W * 2);
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (a[i][j] == 'X') continue;
			//右足が最後
			{
				int cur = c(i, j, 1);
				for (int nh = 0; nh < H; nh++) {
					for (int nw = 0; nw < j; nw++) {
						if (abs(nh - i) + abs(nw - j) > 3) continue;
						if (a[nh][nw] == 'X') continue;
						int t = 0;
						if ('1' <= a[nh][nw] and a[nh][nw] <= '9') t += a[nh][nw] - '0';
						int nxt = c(nh, nw, 0);
						g[cur].emplace_back(nxt, t);
					}
				}
			}
			//左足が最後
			{
				int cur = c(i, j, 0);
				for (int nh = 0; nh < H; nh++) {
					for (int nw = j + 1; nw < W; nw++) {
						if (abs(nh - i) + abs(nw - j) > 3) continue;
						if (a[nh][nw] == 'X') continue;
						int t = 0;
						if ('1' <= a[nh][nw] and a[nh][nw] <= '9') t += a[nh][nw] - '0';
						int nxt = c(nh, nw, 1);
						g[cur].emplace_back(nxt, t);
					}
				}
			}
		}
	}

	ll res = INF;
	vector<int> th, tw;
	vector<int> sh, sw;
	for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
		if (a[i][j] == 'T') {
			th.emplace_back(i);
			tw.emplace_back(j);
		}
		if (a[i][j] == 'S') {
			sh.emplace_back(i);
			sw.emplace_back(j);
		}
	}
	for (int i = 0; i < sh.size(); i++) {
		{
			auto d = dijkstra(c(sh[i], sw[i], 0), g);
			for (int j = 0; j < th.size(); j++) {
				chmin(res, d[c(th[j], tw[j], 0)]);
				chmin(res, d[c(th[j], tw[j], 1)]);
			}
		}
		{
			auto d = dijkstra(c(sh[i], sw[i], 1), g);
			for (int j = 0; j < th.size(); j++) {
				chmin(res, d[c(th[j], tw[j], 1)]);
				chmin(res, d[c(th[j], tw[j], 0)]);
			}
		}
	}
	if (res == INF) res = -1;
	cout << res << "\n";
}

int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int kkt = 89;
	while (kkt) {
		solve();
	}
}


Measured memory_bytes: 3840