//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INF = 123456789;

struct Edge{
  int to, cost;

  Edge(int t, int c): to(t), cost(c)
  {}
  bool operator>(const Edge& rhs) const{
	return cost > rhs.cost;
  }
};

typedef vector< vector<Edge> > Graph;

void Dijkstra(const Graph& edges, vector<int>& d, int s){
  const int V = edges.size();
  priority_queue<PII, vector<PII>, greater<PII> > pq;
  fill(d.begin(), d.end(), INF);
  d[s] = 0;
  pq.push(MP(0,s));

  while(!pq.empty()){
	PII pii = pq.top(); pq.pop();
	int v = pii.second;
	if(d[v] < pii.first) continue;
	for(auto& e:edges[v]){
	  if(d[e.to] > d[v] + e.cost){
		d[e.to] = d[v] + e.cost;
		pq.push(MP(d[e.to], e.to));
	  }
	}
  }
}

int W, H;
int idx(int x, int y, int i = 0){
  return y*W + x + i * W*H;
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while(cin >> W >> H, W){
	Graph G(W*H*2);
	int tab[80][50];
	fill((int*)tab, (int*)tab+80*50, -1);
	VI s, t;
	REP(y,H){
	  REP(x,W){
		char c; cin >> c;
		int cost = 0;
		if(c == 'S'){
		  s.PB(idx(x,y,0));
		  s.PB(idx(x,y,1));		  
		}
		else if(c == 'T'){
		  t.PB(idx(x,y,0));
		  t.PB(idx(x,y,1));
		}
		else if(c == 'X')
		  cost = -1;
		else
		  cost = c - '0';
		tab[y][x] = cost;
	  }
	}
	for(int y=0;y<H;++y){
	  for(int x=0;x<W;++x){
		if(tab[y][x] < 0) continue;
		for(int dx=1;dx<=3;++dx){
		  for(int dy=-(3-dx);dy<=3-dx;++dy){
			int tx = x + dx, ty = y + dy;
			if(0 <= tx && tx < W && 0 <= ty && ty < H && tab[ty][tx] != -1){
			  G[idx(x,y)].PB(Edge(idx(tx,ty,1), tab[ty][tx]));
			}
		  }
		}

		for(int dx=-1;dx>=-3;--dx){
		  for(int dy=-(3+dx);dy<=3+dx;++dy){
			int tx = x + dx, ty = y + dy;
			if(0 <= tx && tx < W && 0 <= ty && ty < H && tab[ty][tx] != -1){
			  G[idx(x,y,1)].PB(Edge(idx(tx,ty,0), tab[ty][tx]));
			}
		  }
		}
	  }
	}

	int ans = INF;
	REP(i,SZ(s)){
	  VI dist(W*H*2);
	  Dijkstra(G, dist, s[i]);
	  REP(j,SZ(t)){
		ans = min(ans, dist[t[j]]);
	  }
	}
	if(ans >= INF) ans = -1;
	cout << ans << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1612