#include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int i,j;
  int w,h;
  while(cin>>w>>h,w||h){
    int a[36][64];
    memset(a,-1,sizeof(a));
    for(i=2;i<h+2;i++){
      for(j=3;j<w+3;j++){
	char s;
	cin>>s;
	if(0){
	}else if(s=='S'){
	  a[j][i]=0;
	}else if(s=='T'){
	  a[j][i]=0;
	}else if(s=='X'){
	  a[j][i]=-1;
	}else{
	  a[j][i]=s-'0';
	}
      }
    }
    int b[36][66][2];
    memset(b,-1,sizeof(b));
    priority_queue<pair<pair<int,int>,pair<int,int> >,vector<pair<pair<int,int>,pair<int,int> > >,greater<pair<pair<int,int>,pair<int,int> > > > c;
    for(i=3;i<w+3;i++){
      if(a[i][h+1]==0){
	c.push(make_pair(make_pair(0,0),make_pair(i,h+1)));
	c.push(make_pair(make_pair(0,1),make_pair(i,h+1)));
      }
    }
    while(c.empty()==0){
      int v,w,x,y;
      v=c.top().first.first;
      w=c.top().first.second;
      x=c.top().second.first;
      y=c.top().second.second;
      if(y==2&&a[x][y]==0)
	break;
      c.pop();
      if(b[x][y][w]==-1||b[x][y][w]>v){
	b[x][y][w]=v;
	if(w){
	  int p,q;
	  p=x-1;
	  q=y;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-2;
	  q=y;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-3;
	  q=y;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-1;
	  q=y+1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-2;
	  q=y+1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-1;
	  q=y-1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-2;
	  q=y-1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-1;
	  q=y+2;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	  p=x-1;
	  q=y-2;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],0),make_pair(p,q)));
	}else{
	  int p,q;
	  p=x+1;
	  q=y;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+2;
	  q=y;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+3;
	  q=y;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+1;
	  q=y+1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+2;
	  q=y+1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+1;
	  q=y-1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+2;
	  q=y-1;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+1;
	  q=y+2;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	  p=x+1;
	  q=y-2;
	  if(a[p][q]!=-1)
	    c.push(make_pair(make_pair(v+a[p][q],1),make_pair(p,q)));
	}
      }
    }
    cout<<(c.empty()?-1:c.top().first.first)<<endl;
  }
  return 0;
}

Measured memory_bytes: 1044