#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
int ijk[61][30][61][30][2];//left,right... 0:last-left 1:last-right
bool v[61][30][61][30][2];
int map[60][30];
int dx[]={2,1,1,0,0,0,-1,-1,-2};
int dy[]={1,1,2,1,2,3,1,2,1};
char str[2];
int main(){
	int a,b;
	while(scanf("%d%d",&b,&a),a+b){
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				scanf("%s",str);
				if(str[0]=='X')map[i][j]=-1;
				else if(str[0]=='S'||str[0]=='T')map[i][j]=0;
				else map[i][j]=str[0]-'0';
			}
		}
		for(int i=0;i<a;i++)
			for(int j=0;j<b;j++){
				ijk[i][j][0][0][0]=-1;
				v[i][j][0][0][0]=false;
				ijk[0][0][i][j][1]=-1;
				v[0][0][i][j][1]=false;
				for(int k=0;k<9;k++){
					if(0<=i+dx[k]&&i+dx[k]<a&&j+dy[k]<b){
						ijk[i][j][i+dx[k]][j+dy[k]][0]=-1;
						v[i][j][i+dx[k]][j+dy[k]][0]=false;
					}
					if(0<=i+dx[k]&&i+dx[k]<a&&j-dy[k]>=0){
						ijk[i+dx[k]][j-dy[k]][i][j][1]=-1;
						v[i+dx[k]][j-dy[k]][i][j][1]=false;;
					}
				}
			}
		priority_queue<pair<pair<int,int>,pair<pair<int,int > ,pair<int,int> > > >Q;
		for(int i=0;i<b;i++){
			if(map[a-1][i]==0){
				ijk[a-1][i][0][0][0]=0;
				Q.push(make_pair(make_pair(0,0),make_pair(make_pair(a-1,i),make_pair(0,0))));
				ijk[0][0][a-1][i][1]=0;
				Q.push(make_pair(make_pair(0,1),make_pair(make_pair(0,0),make_pair(a-1,i))));
			}
		}
		while(Q.size()){
			int cost=-Q.top().first.first;
			int last=Q.top().first.second;
			int Lrow=Q.top().second.first.first;
			int Lcol=Q.top().second.first.second;
			int Rrow=Q.top().second.second.first;
			int Rcol=Q.top().second.second.second;
			Q.pop();
			if(v[Lrow][Lcol][Rrow][Rcol][last])continue;
			v[Lrow][Lcol][Rrow][Rcol][last]=true;
			if(!last){
				for(int i=0;i<9;i++){
					if(0<=Lrow+dx[i]&&Lrow+dx[i]<a&&Lcol+dy[i]<b&&~map[Lrow+dx[i]][Lcol+dy[i]]&&
						(ijk[Lrow][Lcol][Lrow+dx[i]][Lcol+dy[i]][1]==-1||ijk[Lrow][Lcol][Lrow+dx[i]][Lcol+dy[i]][1]>cost+map[Lrow+dx[i]][Lcol+dy[i]])){
						ijk[Lrow][Lcol][Lrow+dx[i]][Lcol+dy[i]][1]=cost+map[Lrow+dx[i]][Lcol+dy[i]];
						Q.push(make_pair(make_pair(-ijk[Lrow][Lcol][Lrow+dx[i]][Lcol+dy[i]][1],1),make_pair(make_pair(Lrow,Lcol),make_pair(Lrow+dx[i],Lcol+dy[i]))));
					}
				}
			}else{
				for(int i=0;i<9;i++){
					if(0<=Rrow+dx[i]&&Rrow+dx[i]<a&&Rcol-dy[i]>=0&&~map[Rrow+dx[i]][Rcol-dy[i]]&&
						(ijk[Rrow+dx[i]][Rcol-dy[i]][Rrow][Rcol][0]==-1||ijk[Rrow+dx[i]][Rcol-dy[i]][Rrow][Rcol][0]>cost+map[Rrow+dx[i]][Rcol-dy[i]])){
						ijk[Rrow+dx[i]][Rcol-dy[i]][Rrow][Rcol][0]=cost+map[Rrow+dx[i]][Rcol-dy[i]];
						Q.push(make_pair(make_pair(-ijk[Rrow+dx[i]][Rcol-dy[i]][Rrow][Rcol][0],0),make_pair(make_pair(Rrow+dx[i],Rcol-dy[i]),make_pair(Rrow,Rcol))));
					}
				}
			}
		}
		int ret=99999999;
		for(int i=0;i<b;i++){
			if(map[0][i]==0){
				for(int j=0;j<9;j++){
					if(0<=dx[j]&&dx[j]<a&&i+dy[j]<b&&~ijk[0][i][dx[j]][i+dy[j]][0])ret=min(ret,ijk[0][i][dx[j]][i+dy[j]][0]);
					if(0<=dx[j]&&dx[j]<a&&i-dy[j]>=0&&~ijk[dx[j]][i-dy[j]][0][i][1])ret=min(ret,ijk[dx[j]][i-dy[j]][0][i][1]);
				//	if(0<=-dx[j]&&-dx[j]<a&&i-dy[j]>=0)printf("%d %d %d %d 0: %d\n",0,i,-dx[j],i-dy[j],ijk[0][i][-dx[j]][i-dy[j]][0]);
				//	if(0<=-dx[j]&&-dx[j]<a&&i+dy[j]<b)printf("%d %d %d %d 1: %d\n",-dx[j],i+dy[j],0,i,1,ijk[-dx[j]][i+dy[j]][0][i][1]);
				}
			}
		}
		if(ret==99999999)printf("-1\n");
		else printf("%d\n",ret);
	}
}

Measured memory_bytes: 0