#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

#define GOAL 10
#define START 0
#define WALL -1

class State{
public:
  int x,y;
  int cost;
  bool left;

  State(){}
  State(int _x,int _y,int _cost,bool _left){
    x = _x;
    y = _y;
    cost = _cost;
    left = _left;
  }

  bool operator<(const State &st)const{
    return cost > st.cost;
  }
};

int w,h;
int t[62][62];

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

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


int main(){
  while(cin>>w>>h,w||h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        char ch;
        cin>>ch;

        if(ch == 'T'){
          t[i][j] = GOAL;
        }
        else if(ch == 'S'){
          t[i][j] = START;
        }
        else if(ch == 'X'){
          t[i][j] = WALL;
        }
        else{
          t[i][j] = ch - '0';
        }
      }
    }

    priority_queue<State> open;
    bool closed[62][62][2];
    memset(closed,0,sizeof(closed));

    for(int i=0;i<w;i++){
      if(t[h-1][i] == START){
        open.push(State(i,h-1,0,true));
        open.push(State(i,h-1,0,false));
      }
    }

    bool goal = false;

    while(!open.empty()){
      State st = open.top(); open.pop();
      if(closed[st.x][st.y][st.left]) continue;
      closed[st.x][st.y][st.left] = true;

      if(t[st.y][st.x] == GOAL){
        cout<<st.cost-GOAL<<endl;
        goal = true;
        break;
      }

      for(int i=0;i<9;i++){
        int nx = st.x + dx[st.left][i];
        int ny = st.y + dy[st.left][i];

        if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] != WALL){
          open.push(State(nx,ny,st.cost+t[ny][nx],!st.left));
        }
      }
    }
    if(!goal) cout<<"-1\n";
  }
}