#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<map>
using namespace std;

// #define int long long

#pragma GCC optimize("O3")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
	 
struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

const long long INF = (long long)1e9;

int ans;

int d[30 * 60 * 30 * 60];
short z[30 * 60 * 30 * 60];
vector<vector<char>> s;
int A , B, C;

// void dijkstra(map<int,int> &d, vector<vector<pair<int,int>>> &G, 
void dijkstra(vector<vector<pair<int,int>>> &G, 
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>& q){
	
	
	// d[s] = 0;
	
	while(!q.empty()){
		pair<int,int> p = q.top();q.pop();
		int v = p.second;
		
		if(d[v] < p.first) continue;
		
		int a = v/A;
		int b = v%A/B;
		int c = v%B/C;
		int D = v%C;
		
		
		if(s[a][b] == 'T'){
			ans = p.first;
			return ;
		}
		if(s[c][D] == 'T') {
			ans = p.first;
			return;
		}
		
		
		for(pair<int,int> e : G[v]){
			if(d[e.first] > d[v] + e.second){
				d[e.first] = d[v] + e.second;
				q.push(make_pair(d[e.first],e.first));
			}
		}
	}	
}

signed main(){
	int con = 0;
	
	while(true){
		con++;
		int w, h;
		// map<int,int> d;
		vector<vector<pair<int,int>>> G;
		
		priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
		cin>>w>>h;
		
		if(!w && !h) break;
		
		s.clear();
		s.resize(h, vector<char>(w));
		
		A = w * h * w;
		B = w * h;
		C = w;
		
		G.resize(w * h * w * h);
		
		// d.resize(G.size(),INF);
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin>>s[i][j];
			}
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(s[i][j] == 'X') continue;
				
				for(int k = 0; k < h; k++){
					for(int l = 0; l < w; l++){// cout<<"<>"<<endl;
					
						if(s[k][l] == 'X') continue;
						
							if(abs(i - k) + abs(j - l) > 3) continue;
					
						int ly = i, lx = j;
						int ry = k, rx = l;
						
						int cur = ly * A + lx * B + ry * C + rx;
						
						bool fr = true;
						bool fl = true;
						
						if(s[ly][lx] != 'S') fr = false;
						
						
						if(s[ry][rx] != 'S') fl = false;
						
						
						d[cur] = INF;
								
						for(int nx = 1; nx <= 3; nx++){
							for(int ny = -2; ny <= 2; ny++){
								int nry = ly + ny;
								int nrx = lx + nx;
								
								int nex = ly * A + lx * B + nry * C + nrx;
								
								if(nry < 0 || nry >= h || nrx < 0 || nrx >= w) continue;
								if(abs(nry - ly) + abs(nrx - lx) > 3) continue;
								if(s[nry][nrx] == 'X') continue;
								// if(s[ly][lx] == 'S') {
									
								// d[nex] = INF;
								if(z[nex] == con) d[nex] = min(d[nex],(int)INF);
								else d[nex] = INF;
								
								if(fr) {
									if(z[nex] == con) {
										if(s[nry][nrx] == 'S' || s[nry][nrx] == 'T') d[nex] = 0;
										else d[nex] = min(d[nex],s[nry][nrx]  - '0');
									} else {
										if(s[nry][nrx] == 'S' || s[nry][nrx] == 'T') d[nex] = 0;
										else d[nex] = s[nry][nrx]  - '0';
									}
									q.push(make_pair(d[nex],nex));
									continue;
								}
								
								z[nex] = con;
								
								if(s[nry][nrx] == 'S' || s[nry][nrx] == 'T') G[cur].push_back({nex, 0});
								// else G[cur].push_back({nex, s[nry][nrx] - '0'});
								else G[cur].push_back({nex, s[nry][nrx] - '0'});
							}
						}
						
						for(int nx = -1; nx >= -3; nx--){
							for(int ny = -2; ny <= 2; ny++){
								int nly = ry + ny;
								int nlx = rx + nx;
								
								int nex = nly * A + nlx * B + ry * C + rx;
								
								if(nly < 0 || nly >= h || nlx < 0 || nlx >= w) continue;
								if(abs(nly - ry) + abs(nlx - rx) > 3) continue;
								if(s[nly][nlx] == 'X') continue;
								
								// if(s[ry][rx] == 'S') {
									
									if(z[nex] == con) d[nex] = min(d[nex],(int)INF);
									else d[nex] = INF;
								
								if(fl) {
									if(z[nex] == con) {
										if(s[nly][nlx] == 'S' || s[nly][nlx] == 'T')d[nex] = 0;
										else d[nex] = min(d[nex],s[nly][nlx] - '0');
									} else {
										if(s[nly][nlx] == 'S' || s[nly][nlx] == 'T')d[nex] = 0;
										else d[nex] = s[nly][nlx] - '0';
									}
									
									q.push(make_pair(d[nex],nex));
									continue;
								}
								
								z[nex] = con;
								
								if(s[nly][nlx] == 'S' || s[nly][nlx] == 'T') G[cur].push_back({nex, 0});
								else G[cur].push_back({nex, s[nly][nlx] - '0'});
							}
						}
						
					}
				}
			}
		}
		
		
		// dijkstra((h-1) * A + 0 * B + (h-1) * C + 1,d, G);
		
		
		ans = INF;
		
		// dijkstra(d, G, q);
		dijkstra(G, q);
		
		// for(int i = 0; i < h; i++){
			// for(int j = 0; j < w; j++){
				// for(int k = 0; k < h; k++){
					// for(int l = 0; l < w; l++){
						// if(s[i][j] != 'T' && s[k][l] != 'T') continue;
						// if(d[i*A + j*B + k * C + l] == 0) continue;
						// ans = min(d[i*A + j*B + k * C + l], ans);
					// }
				// }
			// }
		// }
		
		
		if(ans != INF)cout<<ans<<endl;
		else cout<<-1<<endl;
	}
	
	return 0;
}




Measured memory_bytes: 109628