#include <bits/stdc++.h>
#define loop for(;;)
typedef std::vector<long> LI;
template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
template<> void scan1(std::string& x) { if( not ( std::cin >> x ) ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

template<typename W>
struct N008 {
   typedef std::vector<long> LI;
   typedef std::vector<W>    LW;
   long n, e;
   const LI &ss, &ds;
   const LW &ws;
   std::vector<LI> fi;
   std::vector<LI> ri;
   N008(long n_, const LI& ss_, const LI& ds_, const LW& ws_)
      : n(n_), e(ss_.size()), ss(ss_), ds(ds_), ws(ws_) {
      ri.resize(n+1);
      fi.resize(n+1);
      for(long i = 0; i < e; ++i) {
         fi[ss[i]].push_back(i);
         ri[ds[i]].push_back(i);
      }
   }
};

template<typename W>
struct N013 {
   std::vector<W> xs;
   N013(const N008<W>& g, long s, W inf) : xs(g.n+1, inf) {
      std::priority_queue<std::pair<W, long>> q;
      q.push(std::make_pair(0, s));
      while( not q.empty() ) {
         W d2; long i;
         std::tie(d2, i) = q.top(); q.pop();
         W d = -d2;
         if( not ( d < xs[i] ) ) continue;
         xs[i] = d;
         for(long k : g.fi[i]) {
            W nd = d + g.ws[k];
            if( not ( nd < xs[g.ds[k]] ) ) continue;
            q.push(std::make_pair(-nd, g.ds[k]));
         }
      }
   }
};


struct Solver {
   void solve() {
      long w, h; scan(w, h);
      if( w == 0 and h == 0 ) exit(0);
      getchar();
      std::string sss[64];
      for(long i = 0; i < h; ++i) std::getline(std::cin, sss[i]);
      std::string field[64];
      for(long i = 0; i < h; ++i) {
         for(long k = 0; k < w; ++k) {
            field[i].push_back(sss[i][k*2]);
         }
      }
      LI ss, ds, ws;
      long src = 64 * 64 * 2;
      long dst = 64 * 64 * 2 + 1;
      // left is kotei, right move, code = 0, left move, ocode = 1
      long dx[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},{-1, -1, -1, -1, -1, -2, -2, -2, -3}};
      long dy[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0},{-2, -1, 0, 1, 2, -1, 0, 1, 0}};
      for(long y = 0; y < h; ++y) {
         for(long x = 0; x < w; ++x) {
            for(long k = 0; k < 2; ++k) {
               for(long i = 0; i < 9; ++i) {
                  long nx = x + dx[k][i];
                  long ny = y + dy[k][i];
                  if( not ( 0 <= nx and nx < w and 0 <= ny and ny < h ) ) continue;
                  long w = 0;
                  if( field[ny][nx] == 'X' ) continue;
                  if( '0' <= field[ny][nx] and field[ny][nx] <= '9' ) w = field[ny][nx] - '0';
                  ss.push_back(y * 64 + x + k * 64 * 64);
                  ds.push_back(ny * 64 + nx + (1-k) * 64 * 64);
                  ws.push_back(w);
               }
            }
         }
      }
      for(long y = 0; y < h; ++y) {
         for(long x = 0; x < w; ++x) {
            if( field[y][x] == 'S' ) {
               ss.push_back(src);
               ds.push_back(y*64+x);
               ws.push_back(0);
               ss.push_back(src);
               ds.push_back(y*64+x+64*64);
               ws.push_back(0);
            }
            if( field[y][x] == 'T' ) {
               ss.push_back(y*64+x);
               ds.push_back(dst);
               ws.push_back(0);
               ss.push_back(y*64+x+64*64);
               ds.push_back(dst);
               ws.push_back(0);
            }
         }
      }
      N008<long> graph(64*64*2+10, ss, ds, ws);
      N013<long> dijkstra(graph, src, (1LL << 50));
      long res = dijkstra.xs[dst];
      if( res != (1LL << 50) ) {
         printf("%ld\n", dijkstra.xs[dst]);
      }
      else {
         puts("-1");
      }
   }
};

int main() {
   loop std::shared_ptr<Solver>(new Solver())->solve();
}

Measured memory_bytes: 5176