#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

const int lx[] = { -1, -1, -1, -1, -1, -2, -2, -2, -3 };
const int ly[] = { -2, -1,  0,  1,  2, -1,  0,  1,  0 };
const int rx[] = {  1,  1,  1,  1,  1,  2,  2,  2,  3 };
const int ry[] = { -2, -1,  0,  1,  2, -1,  0,  1,  0 };

class State
{
public:
	int x, y, cost, isLeft;

	State(int xx, int yy, int c, int lr)
		:x(xx), y(yy), cost(c), isLeft(lr) {}
	bool operator < (const State& st) const
	{
		return this->cost > st.cost;
	}
};


int main()
{
	//freopen("D", "r", stdin);
	const int INF = 1 << 30;

	int w, h;
	while (scanf("%d %d", &w, &h) != EOF) {
		if (w == 0 && h == 0)
			break;

		static int cost[61][31][2];
		static bool visited[61][31][2];
		for (int i = 0; i < 61; ++i) {
			for (int j = 0; j < 31; ++j) {
				cost[i][j][0] = INF;
				cost[i][j][1] = INF;
				visited[i][j][0] = false;
				visited[i][j][1] = false;
			}
		}

		char f[61][31];
		priority_queue<State> que;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> f[i][j];
				//scanf("%d", &f[i][j]);
				if (f[i][j] == 'S') {
					que.push(State(j, i, 0, 1));
					que.push(State(j, i, 0, 0));
				}
			}
		}

		int ans = INF;
		while (!que.empty()) {
			const State st = que.top();
			que.pop();

			//printf("(%d, %d) (%d, %d)\n", st.left.x, st.left.y, st.right.x, st.right.y);

			if (f[st.y][st.x] == 'T') {
					ans = st.cost;
					break;
			}

			if (visited[st.y][st.x][st.isLeft] ||
				cost[st.y][st.x][st.isLeft] < st.cost)
				continue;

			visited[st.y][st.x][st.isLeft] = true;

			if (st.isLeft == 1) {
				for (int i = 0; i < 9; ++i) {
					int xx = st.x + rx[i];
					int yy = st.y + ry[i];
					if (xx < 0 || xx >= w || yy < 0 || yy >= h || f[yy][xx] == 'X')
						continue;
					int c = st.cost;
					if (isdigit(f[yy][xx]))
						c += (f[yy][xx] - '0');
					if (cost[yy][xx][0] > c) {
						cost[yy][xx][0] = c;
						que.push(State(xx, yy, c, 0));
					}
				}
			} else {
				for (int i = 0; i < 9; ++i) {
					int xx = st.x + lx[i];
					int yy = st.y + ly[i];
					if (xx < 0 || xx >= w || yy < 0 || yy >= h || f[yy][xx] == 'X')
						continue;
					int c = st.cost;
					if (isdigit(f[yy][xx]))
						c += (f[yy][xx] - '0');
					if (cost[yy][xx][1] > c) {
						cost[yy][xx][1] = c;
						que.push(State(xx, yy, c, 1));
					}
				}
			}
		}

		if (ans == INF)
			ans = -1;
		printf("%d\n", ans);
	}

	return 0;
}