#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

int dp[60][30][60][30][2];

struct Node {
	int ly, lx, ry, rx, nx, cost;
	Node(const int lly, const int llx, const int rry, const int rrx, const int nnx, const int ccost) {
		ly = lly, lx = llx, ry = rry, rx = rrx, nx = nnx, cost = ccost;
	}
	void debug() {
		cout << ly << " " << lx << " " << ry << " " << rx << " " << nx << " " << cost << endl;
	}
	bool operator<(const Node&n)const {
		return cost > n.cost;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> W >> H, H) {
		vector<vector<char>>field(H, vector<char>(W));
		for (auto &i : field)for (auto &j : i)cin >> j;
		reverse(field.begin(), field.end());
		int ans = MOD;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				for (int k = 0; k < H; k++) {
					for (int l = 0; l < W; l++) {
						for (int m = 0; m < 2; m++)dp[i][j][k][l][m] = MOD;
					}
				}
			}
		}
		priority_queue<Node>PQ;
		for (int k = 0; k < W; k++) {
			if (field[0][k] != 'S')continue;
			for (int j = -3; j < 0; j++) {
				for (int i = -3 - j; i <= j + 3; i++) {
					if (0 + i < 0 || 0 + i >= H || k + j < 0 || k + j >= W)continue;
					if (field[0 + i][k + j] == 'X')continue;
					int add = 0;
					if (field[0 + i][k + j] >= '0'&&field[0 + i][k + j] <= '9') {
						add = field[0 + i][k + j] - '0';
					}
					if (dp[0 + i][k + j][0][k][1] > 0 + add) {
						dp[0 + i][k + j][0][k][1] = 0 + add;
						PQ.push(Node(0 + i, k + j, 0, k, 1, 0 + add));
					}
				}
				for (int j = 1; j <= 3; j++) {
					for (int i = j - 3; i <= 3 - j; i++) {
						if (0 + i < 0 || 0 + i >= H || k + j < 0 || k + j >= W)continue;
						if (field[0 + i][k + j] == 'X')continue;
						int add = 0;
						if (field[0 + i][k + j] >= '0'&&field[0 + i][k + j] <= '9') {
							add = field[0 + i][k + j] - '0';
						}
						if (dp[0][k][0 + i][k + j][0] > 0 + add) {
							dp[0][k][0 + i][k + j][0] = 0 + add;
							PQ.push(Node(0, k, 0 + i, k + j, 0, 0 + add));
						}
					}
				}
			}
		}
		while (!PQ.empty()) {
			auto c = PQ.top();
			//c.debug();
			PQ.pop();
			if (c.nx == 0) {
				for (int j = -3; j < 0; j++) {
					for (int i = -3 - j; i <= j + 3; i++) {
						if (c.ry + i < 0 || c.ry + i >= H || c.rx + j < 0 || c.rx + j >= W)continue;
						if (field[c.ry + i][c.rx + j] == 'X')continue;
						int add = 0;
						if (field[c.ry + i][c.rx + j] >= '0'&&field[c.ry + i][c.rx + j] <= '9') {
							add = field[c.ry + i][c.rx + j] - '0';
						}
						if (dp[c.ry + i][c.rx + j][c.ry][c.rx][1] > c.cost + add) {
							dp[c.ry + i][c.rx + j][c.ry][c.rx][1] = c.cost + add;
							PQ.push(Node(c.ry + i, c.rx + j, c.ry, c.rx, 1, c.cost + add));
						}
					}
				}
			}
			else {
				for (int j = 1; j <= 3; j++) {
					for (int i = j - 3; i <= 3 - j; i++) {
						if (c.ly + i < 0 || c.ly + i >= H || c.lx + j < 0 || c.lx + j >= W)continue;
						if (field[c.ly + i][c.lx + j] == 'X')continue;
						int add = 0;
						if (field[c.ly + i][c.lx + j] >= '0'&&field[c.ly + i][c.lx + j] <= '9') {
							add = field[c.ly + i][c.lx + j] - '0';
						}
						if (dp[c.ly][c.lx][c.ly + i][c.lx + j][0] > c.cost + add) {
							dp[c.ly][c.lx][c.ly + i][c.lx + j][0] = c.cost + add;
							PQ.push(Node(c.ly, c.lx, c.ly + i, c.lx + j, 0, c.cost + add));
						}
					}
				}
			}
		}
		for (int i = 0; i < W; i++) {
			if (field[H - 1][i] == 'T') {
				for (int j = 0; j < H; j++) {
					for (int k = 0; k < W; k++) {
						for (int l = 0; l < 2; l++) {
							ans = min(ans, dp[H - 1][i][j][k][l]);
							ans = min(ans, dp[j][k][H - 1][i][l]);
						}
					}
				}
			}
		}
		if (ans == MOD)ans = -1;
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 28576