#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <functional>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>


using namespace std;

const int INF = 1e9 + 10;

int w, h;

int field[61][31];

struct Search {
  int y, x;
  int foot;
  int cost;
  Search(int y, int x, int foot, int c): y(y), x(x), foot(foot), cost(c) { }
};

int main() {
  while (true) {
    cin >> w  >> h;
    if (w + h == 0) {
      break;
    }

    int min_cost[2][61][31];
    for (int i = 0; i < 61; i++) {
      for (int j = 0; j < 31; j++) {
        min_cost[0][i][j] = INF;
        min_cost[1][i][j] = INF;
      }
    }


    queue<Search> que;


    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char c;
        cin >> c;
        if (c == 'X') {
          field[i][j] = -1;
        } else if (c == 'S') {
          field[i][j] = 0;
          min_cost[0][i][j] = 0;
          min_cost[1][i][j] = 0;
          que.push(Search(i, j, 0, 0));
          que.push(Search(i, j, 1, 0));
        } else if (c == 'T') {
          field[i][j] = 10;
        } else {
          field[i][j] = c - '0';
        }
      }
    }

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

    int ans = INF;

    while (!que.empty()) {
      Search s = que.front(); que.pop();
      int next_foot = !s.foot;
      for (int i = 0; i < 9; i++) {
        int ny = s.y + dy[next_foot][i];
        int nx = s.x + dx[next_foot][i];
        
        if (ny < 0 || ny >= h) {
          continue;
        }
        if (nx < 0 || nx >= w) {
          continue;
        }
        if (field[ny][nx] == -1) {
          continue;
        }
        if (field[ny][nx] == 10) {
          ans = min(ans, s.cost);
          continue;
        }
        int cost = s.cost + field[ny][nx];
        if (cost < min_cost[next_foot][ny][nx]) {
          min_cost[next_foot][ny][nx] = cost;
          que.push(Search(ny, nx, next_foot, cost));
        }
      }
    }
    
    if (ans == INF) {
      ans = -1;
    }

    cout << ans << endl;
  }

  return 0;
}