//#include <bits/stdc++.h>
//#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>
 
 
#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define DINF 100000000.0
#define PI 3.14159265358979
#define MAXI 7500000
 
using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

 
int dy[5] = { 0,0,1,-1,0 };
int dx[5] = { 1,-1,0,0,0 };

struct edge{long long int to,cost;};

 
int main(void){

	while(1){
		
		long long int w,h;

		cin>>w>>h;
		if(w==0&&h==0){
			break;
		}

		vector<long long int> v(3600,LINF);
		vector<edge> G[3600];
		long long int d[3600];
		long long int mini=LINF;
		queue<P> start; //y,x
		vector<P> end;
		P tmp;

		char s[61][31];
		LFOR(i,1,h){
			LFOR(j,1,w){
				cin>>s[i][j];
				if(s[i][j]=='S'){
					start.push(make_pair(i,j));
				}
				if(s[i][j]=='T'){
					end.push_back(make_pair(i,j));
				}
			}
		}

		edge ED;		

		LFOR(y,1,h){
			LFOR(x,1,w){
				LFOR(l,0,1){
					if(l==0){//左
						LFOR(rx,1,3){
							LFOR(ry,-2,2){
								if(rx+abs(ry)<=3){
									if(x+rx>=1&&x+rx<=w&&y+ry>=1&&y+ry<=h){
										if(s[y+ry][x+rx]!='X'){
											ED.to=2*(y+ry-1)*w+2*(x+rx-1)+1;
											if(s[y+ry][x+rx]=='T'||s[y+ry][x+rx]=='S'){
												ED.cost=0;
											}else{
												ED.cost=(s[y+ry][x+rx]-'0');
											}
											G[2*(y-1)*w+2*(x-1)].push_back(ED);
										}
									}
								}
							}
						}
					}else{//右
						LFOR(lx,-3,-1){
							LFOR(ly,-2,2){
								if(abs(lx)+abs(ly)<=3){
									if(x+lx<=w&&x+lx>=1&&y+ly>=1&&y+ly<=h){
										if(s[y+ly][x+lx]!='X'){
											ED.to=2*(y+ly-1)*w+2*(x+lx-1);
											if(s[y+ly][x+lx]=='T'||s[y+ly][x+lx]=='S'){
												ED.cost=0;
											}else{
												ED.cost=(s[y+ly][x+lx]-'0');
											}
											G[2*(y-1)*w+2*(x-1)+l].push_back(ED);
										}
									}
								}
							}
						}
					}
				}
			}
		}

		while(!start.empty()){
			tmp=start.front();
			start.pop();
			LFOR(l,0,1){
				priority_queue<P,vector<P>,greater<P> > que;
				LFOR(i,0,3599){
					d[i]=LINF;
				}
				d[2*(tmp.first-1)*w+2*(tmp.second-1)+l]=0;
				que.push(P(0,2*(tmp.first-1)*w+2*(tmp.second-1)+l));


				while(!que.empty()){
					P p=que.top();
					que.pop();
					long long int v=p.second;
					if(d[v]<p.first){
						continue;
					}
					if(G[v].size()!=0){
						LFOR(i,0,G[v].size()-1){
						edge e=G[v][i];
						if(d[e.to]>d[v]+e.cost){
							d[e.to]=d[v]+e.cost;
							que.push(P(d[e.to],e.to));
						}
					}
					}
					
				}

				
				LFOR(i,0,end.size()-1){
					LFOR(k,0,1){
						mini=min(mini,d[2*(end[i].first-1)*w+2*(end[i].second-1)+k]);
					}
				}

			}
			
		}
		
		if(mini!=LINF){
			cout<<mini<<endl;
		}else{
			cout<<-1<<endl;
		}



	}

}



Measured memory_bytes: 3976