#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 30;
char field[100][100];
int memo[100][100][2];

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

int main(){
  int w, h;
  while(cin >> w >> h && (w || h)){
    fill(&memo[0][0][0], &memo[99][99][1]+1, INF);
    priority_queue<PPP, vector<PPP>, greater<PPP> > que;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> field[j][i];
	if(field[j][i] == 'S'){
	  field[j][i] = '0';
	  memo[j][i][0] = memo[j][i][1] = 0;
	  que.push(PPP(0, PP(0, P(j, i))));
	  que.push(PPP(0, PP(1, P(j, i))));
	}
      }
    }

    bool ok = false;
    while(!que.empty()){
      PPP ppp = que.top();
      que.pop();
      int dis = ppp.first;
      int foot = ppp.second.first;
      int next = foot ^ 1;
      int x = ppp.second.second.first;
      int y = ppp.second.second.second;
      if(dis > memo[x][y][foot]) continue;
      if(field[x][y] == 'T'){
	cout << memo[x][y][foot] << endl;
	ok = true;
	break;
      }
      for(int i = 0; i < 9; i++){
	int nx, ny;
	if(foot == 0) nx = x + dx[i];
	else nx = x - dx[i];
	ny = y + dy[i];
	if(0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] != 'X'){
	  int cost;
	  if(isdigit(field[nx][ny])) cost = field[nx][ny] - '0';
	  else cost = 0;
	  if(memo[nx][ny][next] > memo[x][y][foot] + cost){
	    //	    cout << x << " " << y  << " " << nx << " " << ny << endl;
	    memo[nx][ny][next] = memo[x][y][foot] + cost;
	    que.push(PPP(memo[nx][ny][next], PP(next, P(nx, ny))));
	  }
	}
      }
    }
    if(!ok) cout << -1 << endl;
  }
  return 0;
}
	