#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <cctype>
using namespace std;

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

class Point {
public:
  int x;
  int y;
  Point() {}
  Point(int x, int y) : x(x), y(y) {}
  bool operator <(const Point &p) const {
    if(x != p.x) return x < p.x;
    return y < p.y;
  }
};

class State {
public:
  int time;
  Point left;
  Point right;
  State() {
    time = 0;
  }
  bool operator <(const State &s) const {
    if(time != s.time) return time > s.time;
    if(left < s.left || s.left < left) return left < s.left;
    return right < s.right;
  }
};

class VisSta {
public:
  Point left;
  Point right;
  VisSta(State &s) {
    left = s.left;
    right = s.right;
  }

  bool operator <(const VisSta &vs) const {
    if(left < vs.left || vs.left < left) return left < vs.left;
    return right < vs.right;
  }
};

int solve() {
  State s;
  priority_queue<State> que;
  set<VisSta> vis;
  const int rr[9][2] = {{1,-2},{1,-1},{1,0},{1,1},{1,2},
			{2,-1},{2,0},{2,1},{3,0}};
  const int rl[9][2] = {{-1,-2},{-1,-1},{-1,0},{-1,1},{-1,2},
			{-2,-1},{-2,0},{-2,1},{-3,0}};

  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      if(M[i][j] == 'S') {
	s.left = Point(j, i);
	s.right = Point(j, i);
	que.push(s);
      }
    }
  }

  while(!que.empty()) {
    s = que.top();
    que.pop();

    if(M[s.left.y][s.left.x] == 'T' || M[s.right.y][s.right.x] == 'T') {
      //cout << s.left.x << ", " << s.left.y << " | " << s.right.x << ", " << s.right.y << endl;
      return s.time;
    }
    
    for(int k = 0; k < 9; ++k) {
      State newS = s;
      int nx = s.left.x + rr[k][0];
      int ny = s.left.y + rr[k][1];
      if(nx < 0 || nx >= w) continue;
      if(ny < 0 || ny >= h) continue;
      if(nx == s.right.x && ny == s.right.y) continue;
      if(M[ny][nx] == 'X') continue;
      newS.right = Point(nx, ny);
      if(isdigit(M[ny][nx])) newS.time += M[ny][nx] - '0';
      VisSta vs(newS);
      if(vis.find(vs) == vis.end()) {
        vis.insert(vs);
        que.push(newS);
      }
    }

    for(int k = 0; k < 9; ++k) {
      State newS = s;
      int nx = s.right.x + rl[k][0];
      int ny = s.right.y + rl[k][1];
      if(nx < 0 || nx >= w) continue;
      if(ny < 0 || ny >= h) continue;
      if(nx == s.left.x && ny == s.left.y) continue;
      if(M[ny][nx] == 'X') continue;
      newS.left = Point(nx, ny);
      if(isdigit(M[ny][nx])) newS.time += M[ny][nx] - '0';
      VisSta vs(newS);
      if(vis.find(vs) == vis.end()) {
        vis.insert(vs);
        que.push(newS);
      }
    }
  }
  return -1;
}

int main() {
  while((cin >> w >> h) && (w || h)) {
    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
	cin >> M[i][j];
      }
    }
    cout << solve() << endl;
  }
}