#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++)
				for(int k=0;k<a;k++)
					for(int l=0;l<b;l++){
						ijk[i][j][k][l][0]=ijk[i][j][k][l][1]=-1;
						v[i][j][k][l][0]=v[i][j][k][l][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<a;j++){
					for(int k=0;k<b;k++){
						if(~ijk[0][i][j][k][0])ret=min(ret,ijk[0][i][j][k][0]);
						if(~ijk[j][k][0][i][1])ret=min(ret,ijk[j][k][0][i][1]);
					}
				}
			}
		}
		if(ret==99999999)printf("-1\n");
		else printf("%d\n",ret);
	}
}