#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);i++)
#define rep0(i,n) for(i=0;i<(n);i++)
#define repp(i,m,n) for(int i=(m);i<(n);i++)

#define PUTLINE cout<<__LINE__<<endl;

int cost[70][40][2];
string s[70][40];

class State{
public:
	int w,h,foot,cost;
	bool operator>(State state)const{return cost>state.cost;}
};

int main() {
	int w,h;
	while(true){
		cin>>w>>h;
		if(w==0&&h==0)break;
		rep(i,h)rep(j,w){
			cin>>s[i][j];
			cost[i][j][0]=cost[i][j][1]=100100100;
		}
		priority_queue<State,vector<State>,greater<State> > que;
		rep(j,w){
			if(s[h-1][j]=="S"){
				State buf;
				buf.h=h-1;
				buf.w=j;
				buf.cost=0;
				buf.foot=0;
				que.push(buf);
				buf.foot=1;
				que.push(buf);
			}
		}
		int result=100100100;
		while(!que.empty()){
			State state=que.top();
			que.pop();
//cout<<state.h<<" "<<state.w<<" "<<state.cost<<" "<<state.foot<<endl;
			if(state.cost>=cost[state.h][state.w][state.foot])continue;
			if(s[state.h][state.w]=="X")continue;
			chmin(cost[state.h][state.w][state.foot],state.cost);
			State next;
			next.foot=state.foot^1;
			int i,j;
			if(state.foot==0){
				for(i=-2;i<=2;i++){
					for(j=1;j<=3;j++){
						if(abs(i)+j>3)continue;
						if(!(0<=state.h+i&&state.h+i<h&&0<=state.w+j&&state.w+j<w))continue;
						next.h=state.h+i;
						next.w=state.w+j;
						if(s[next.h][next.w]=="S")continue;
						if(s[next.h][next.w]=="T"){
							chmin(result,state.cost);
							continue;
						}
						next.cost=state.cost+s[next.h][next.w][0]-'0';
						que.push(next);
					}
				}
			}
			if(state.foot==1){
				for(i=-2;i<=2;i++){
					for(j=-3;j<=-1;j++){
						if(abs(i)-j>3)continue;
						if(!(0<=state.h+i&&state.h+i<h&&0<=state.w+j&&state.w+j<w))continue;
						if(s[next.h][next.w]=="T"){
							chmin(result,state.cost);
							continue;
						}
						next.h=state.h+i;
						next.w=state.w+j;
						if(s[next.h][next.w]=="S")continue;
						next.cost=state.cost+s[next.h][next.w][0]-'0';
						que.push(next);
					}
				}
			}
		}
		if(result==100100100)cout<<-1<<endl;
		else cout<<result<<endl;
	}
	return 0;
}