#include <bits/stdc++.h>
using namespace std;
int INF = 100000000;
int main(){
  while (1){
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0){
      break;
    }
    vector<vector<char>> s(h, vector<char>(w));
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        cin >> s[i][j];
      }
    }
    map<int, vector<pair<int, int>>> E;
    int M = h * w * h * w * 2;
    for (int ly = 0; ly < h; ly++){
      for (int lx = 0; lx < w; lx++){
        for (int ry = 0; ry < h; ry++){
          for (int rx = 0; rx < w; rx++){
            if (lx < rx && abs(lx - rx) + abs(ly - ry) <= 3){
              if (s[ly][lx] != 'X' && s[ry][rx] != 'X'){
                int ul = ((ly * w + lx) * h * w + (ry * w + rx)) * 2;
                for (int rx2 = lx + 1; rx2 <= lx + 3; rx2++){
                  for (int ry2 = ly - (3 - (rx2 - lx)); ry2 <= ly + (3 - (rx2 - lx)); ry2++){
                    if (0 <= rx2 && rx2 < w && 0 <= ry2 && ry2 < h){
                      if (s[ry2][rx2] != 'X'){
                        int v = ((ly * w + lx) * h * w + (ry2 * w + rx2)) * 2 + 1;
                        int c;
                        if (s[ry2][rx2] == 'S' || s[ry2][rx2] == 'T'){
                          c = 0;
                        } else {
                          c = s[ry2][rx2] - '0';
                        }
                        E[ul].push_back(make_pair(c, v));
                      }
                    }
                  }
                }
                int ur = ((ly * w + lx) * h * w + (ry * w + rx)) * 2 + 1;
                for (int lx2 = rx - 3; lx2 <= rx - 1; lx2++){
                  for (int ly2 = ry - (3 - (rx - lx2)); ly2 <= ry + (3 - (rx - lx2)); ly2++){
                    if (0 <= lx2 && lx2 < w && 0 <= ly2 && ly2 < h){
                      if (s[ly2][lx2] != 'X'){
                        int v = ((ly2 * w + lx2) * h * w + (ry * w + rx)) * 2;
                        int c;
                        if (s[ly2][lx2] == 'S' || s[ly2][lx2] == 'T'){
                          c = 0;
                        } else {
                          c = s[ly2][lx2] - '0';
                        }
                        E[ur].push_back(make_pair(c, v));
                      }
                    }
                  }
                }
                if (s[ly][lx] == 'S'){
                  int c;
                  if (s[ry][rx] == 'S' || s[ry][rx] == 'T'){
                    c = 0;
                  } else {
                    c = s[ry][rx] - '0';
                  }
                  E[M].push_back(make_pair(c, ur));
                }
                if (s[ry][rx] == 'S'){
                  int c;
                  if (s[ly][lx] == 'S' || s[ly][lx] == 'T'){
                    c = 0;
                  } else {
                    c = s[ly][lx] - '0';
                  }
                  E[M].push_back(make_pair(c, ul));
                }
                if (s[ly][lx] == 'T'){
                  E[ul].push_back(make_pair(0, M + 1));
                }
                if (s[ry][rx] == 'T'){
                  E[ur].push_back(make_pair(0, M + 1));
                }
              }
            }
          }
        }
      }
    }
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push(make_pair(0, M));
    map<int, int> d;
    while (!pq.empty()){
      int c = pq.top().first;
      int v = pq.top().second;
      pq.pop();
      if (!d.count(v)){
        d[v] = c;
        for (auto P : E[v]){
          int w = P.second;
          pq.push(make_pair(c + P.first, w));
        }
      }
    }
    if (!d.count(M + 1)){
      cout << -1 << endl;
    } else {
      cout << d[M + 1] << endl;
    }
  }
}


Measured memory_bytes: 10276