#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>

using namespace std;

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

const int inf=1<<29;

int w,h;
char s[30][60];


int dist[2][30][60][30][60];


// cost l foot r foot isl

struct state{
	int cost;
	int last;
	int lw,lh;
	int rw,rh;
	bool operator>(const state &arg)const{
		return cost > arg.cost;
	}
};

int dx[9]={1,1,1,1,1,2,2,2,3};
int dy[9]={-2,-1,0,1,2,-1,0,1,0};

inline bool valid(int ww,int hh){
	return 0<=ww&&ww<w&&0<=hh&&hh<h;
}

int dijkstra(){
	rep(a,2)rep(b,30)rep(c,60)rep(d,30)rep(e,60)
		dist[a][b][c][d][e]=inf;

	priority_queue <state,vector<state>,greater<state> >q;

	state init;
	init.cost=0;
	init.lw=-1,init.lh=-1;
	init.rw=-1,init.rh=-1;

	rep(i,w){
		if(s[i][h-1]=='S'){
			state initl=init;
			state initr=init;
			initl.lw=i,initl.lh=h-1,initl.last=0;
			initr.rw=i,initr.rh=h-1,initr.last=1;
			q.push(initl);
			q.push(initr);
		}
	}

	while(!q.empty()){
		state cur=q.top();q.pop();
	//	cerr << cur.last << " " << cur.lw << " " << cur.lh << " " << cur.rw << " " <<cur.rh << endl;
	//	cerr << cur.cost << endl;

		if(dist[cur.last][cur.lw][cur.lh][cur.rw][cur.rh]<cur.cost)
			continue;

		if(s[cur.lw][cur.lh]=='T'||s[cur.rw][cur.rh]=='T')
			return cur.cost;

		if(cur.last){
			rep(i,9){
				state next=cur;
				next.last^=1;
				next.lw=next.rw-dx[i];
				next.lh=next.rh+dy[i];
				if(!valid(next.lw,next.lh))
					continue;

				if(s[next.lw][next.lh]=='X')
					continue;

				if(isdigit(s[next.lw][next.lh])) next.cost+=s[next.lw][next.lh]-'0';

				if(dist[next.last][next.lw][next.lh][next.rw][next.rh]>next.cost){
					dist[next.last][next.lw][next.lh][next.rw][next.rh]=next.cost;
					q.push(next);
				}
			}
		}else{
			rep(i,9){
				state next=cur;
				next.last^=1;
				next.rw=next.lw+dx[i];
				next.rh=next.lh+dy[i];
				if(!valid(next.rw,next.rh))
					continue;

				if(s[next.rw][next.rh]=='X')
					continue;

				if(isdigit(s[next.rw][next.rh])) next.cost+=s[next.rw][next.rh]-'0';

				if(dist[next.last][next.lw][next.lh][next.rw][next.rh]>next.cost){
					dist[next.last][next.lw][next.lh][next.rw][next.rh]=next.cost;
					q.push(next);
				}
			}
		}
	}
	return -1;
}

int main(void){
	while(cin >> w >> h){
		if(w==0&&h==0)
			break;
		rep(j,h)rep(i,w) cin >> s[i][j];
		cout << dijkstra() << endl;
	}
}