#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string.h>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

enum Foot{
	Left,
	Right,
};

int H,W;
int**** min_Time;

int diff_row_right[9] = {-2,-1,-1,0,0,0,1,1,2},diff_col_right[9] = {1,1,2,1,2,3,1,2,1};
int diff_row_left[9] = {-2,-1,-1,0,0,0,1,1,2},diff_col_left[9] = {-1,-2,-1,-3,-2,-1,-2,-1,-1};

struct Info{
	Info(){
		left_row = left_col = right_row = right_col = time = 0;
		foot = Left;
	}

	Info(int arg_left_row,int arg_left_col,int arg_right_row,int arg_right_col,int arg_time,Foot arg_foot){
		left_row = arg_left_row;
		left_col = arg_left_col;
		right_row = arg_right_row;
		right_col = arg_right_col;
		time = arg_time;
		foot = arg_foot;
	}
	void set(int arg_left_row,int arg_left_col,int arg_right_row,int arg_right_col,int arg_time,Foot arg_foot){
		left_row = arg_left_row;
		left_col = arg_left_col;
		right_row = arg_right_row;
		right_col = arg_right_col;
		time = arg_time;
		foot = arg_foot;
	}
	int left_row,left_col,right_row,right_col,time;
	Foot foot;
};

struct Cell{
	Cell(){
		row = col = 0;
	}
	Cell(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	int row,col;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){

	for(int a = 0; a < H; a++){
		for(int b = 0; b < W; b++){
			for(int c = 0; c < H; c++){
				for(int d = 0; d < W; d++){
					min_Time[a][b][c][d] = BIG_NUM;
				}
			}
		}
	}

	vector<Cell> V;

	char map[H][W+1];
	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++){
			scanf("%s",&map[i][k]);
			if(map[i][k] == 'S'){
				V.push_back(Cell(i,k));
				map[i][k] = '0';
			}
		}
	}

	queue<Info> Q;

	int first_left_row,first_left_col,first_right_row,first_right_col,add_time;

	for(int a = 0; a < V.size(); a++){

		first_left_row = V[a].row;
		first_left_col = V[a].col;

		for(int i = 0; i < 9; i++){
			first_right_row = first_left_row + diff_row_right[i];
			first_right_col = first_left_col + diff_col_right[i];

			if(rangeCheck(first_right_row,first_right_col) == true && map[first_right_row][first_right_col] != 'X'){
				if(map[first_right_row][first_right_col] >= '0' && map[first_right_row][first_right_col] <= '9'){
					add_time = map[first_right_row][first_right_col] - '0';
				}else{
					add_time = 0;
				}
				if(min_Time[first_left_row][first_left_col][first_right_row][first_right_col] > add_time){
					min_Time[first_left_row][first_left_col][first_right_row][first_right_col] = add_time;
					Q.push(Info(first_left_row,first_left_col,first_right_row,first_right_col,add_time,Right));
				}
			}
		}

		first_right_row = V[a].row;
		first_right_col = V[a].col;

		for(int i = 0; i < 9; i++){
			first_left_row = first_right_row + diff_row_left[i];
			first_left_col = first_right_col + diff_col_left[i];

			if(rangeCheck(first_left_row,first_left_col) == true && map[first_left_row][first_left_col] != 'X'){
				if(map[first_left_row][first_left_col] >= '0' && map[first_left_row][first_left_col] <= '9'){
					add_time = map[first_left_row][first_left_col] - '0';
				}else{
					add_time = 0;
				}
				if(min_Time[first_left_row][first_left_col][first_right_row][first_right_col] > add_time){
					min_Time[first_left_row][first_left_col][first_right_row][first_right_col] = add_time;
					Q.push(Info(first_left_row,first_left_col,first_right_row,first_right_col,add_time,Left));
				}
			}
		}
	}

	int ans = BIG_NUM,next_left_row,next_left_col,next_right_row,next_right_col;

	while(!Q.empty()){

		if(map[Q.front().left_row][Q.front().left_col] == 'T' || map[Q.front().right_row][Q.front().right_col] == 'T'){
			ans = min(ans,Q.front().time);
			Q.pop();
		}else if(Q.front().time > min_Time[Q.front().left_row][Q.front().left_col][Q.front().right_row][Q.front().right_col]){
			Q.pop();
		}else{

			if(Q.front().foot == Left){
				for(int i = 0; i < 9; i++){
					next_right_row = Q.front().left_row + diff_row_right[i];
					next_right_col = Q.front().left_col + diff_col_right[i];

					if(rangeCheck(next_right_row,next_right_col) == true && map[next_right_row][next_right_col] != 'X'){
						if(map[next_right_row][next_right_col] >= '0' && map[next_right_row][next_right_col] <= '9'){
							add_time = map[next_right_row][next_right_col]-'0';
						}else{
							add_time = 0;
						}
						if(min_Time[Q.front().left_row][Q.front().left_col][next_right_row][next_right_col] > Q.front().time+add_time){
							min_Time[Q.front().left_row][Q.front().left_col][next_right_row][next_right_col] = Q.front().time+add_time;
							Q.push(Info(Q.front().left_row,Q.front().left_col,next_right_row,next_right_col,Q.front().time+add_time,Right));
						}
					}
				}
			}else{ //Q.front().foot == Right
				for(int i = 0; i < 9; i++){
					next_left_row = Q.front().right_row + diff_row_left[i];
					next_left_col = Q.front().right_col + diff_col_left[i];

					if(rangeCheck(next_left_row,next_left_col) == true && map[next_left_row][next_left_col] != 'X'){
						if(map[next_left_row][next_left_col] >= '0' && map[next_left_row][next_left_col] <= '9'){
							add_time = map[next_left_row][next_left_col]-'0';
						}else{
							add_time = 0;
						}
						if(min_Time[next_left_row][next_left_col][Q.front().right_row][Q.front().right_col] > Q.front().time+add_time){
							min_Time[next_left_row][next_left_col][Q.front().right_row][Q.front().right_col] = Q.front().time+add_time;
							Q.push(Info(next_left_row,next_left_col,Q.front().right_row,Q.front().right_col,Q.front().time+add_time,Left));
						}
					}
				}
			}

			Q.pop();
		}

	}

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

int main(){

	min_Time = new int***[60];
	for(int i = 0; i < 60; i++){
		min_Time[i] = new int**[30];
		for(int k = 0; k < 30; k++){
			min_Time[i][k] = new int*[60];
			for(int p = 0; p < 60; p++){
				min_Time[i][k][p] = new int[30];
				//for(int q = 0; q < 30; q++)min_Time[i][k][p][q] = new int[2];
			}
		}
	}

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

   return 0;
}

Measured memory_bytes: 17076