#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

const int INF = INT_MAX / 2;

struct Edge
{
	int y, x, cost;
	Edge( int y, int x, int c ) : y( y ), x( x ), cost( c )
	{
		return;
	}
};

struct State
{
	int y, x, foot;
	State( int y, int x, int f ) : y( y ), x( x ), foot( f )
	{
		return;
	}

	bool operator < ( const State &a ) const
	{
		return true;
	}
};

typedef vector<Edge> VE;
typedef vector<VE> VVE;
typedef vector<VVE> VVVE;

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int w, h;
		cin >> w >> h;

		if ( !( w | h ) )
		{
			break;
		}

		vector< vector<char> > board( h, vector<char>( w ) );
		priority_queue< pair<int,State>, vector< pair<int,State> >, greater< pair<int,State> > > que;
		vector<VVI> distance( h, VVI( w, VI( 2, INF ) ) );
		vector<PII> goals;

		REP( i, 0, h )
		{
			REP( j, 0, w )
			{
				cin >> board[i][j];
				
				switch ( board[i][j] )
				{
					case 'S':
						que.push( MP( 0, State( i, j, 0 ) ) );
						que.push( MP( 0, State( i, j, 1 ) ) );
						distance[i][j][0] = distance[i][j][1] = 0;
						board[i][j] = '0';
						break;
					case 'T':
						goals.PB( MP( i, j ) );
						board[i][j] = '0';
						break;
				}
			}
		}
		
		vector<VVVE> G( h, VVVE( w, VVE( 2 ) ) );
		REP( y1, 0, h )
		{
			REP( x1, 0, w )
			{
				if ( !( 0 <= y1 && y1 < h && 0 <= x1 && x1 < w ) || board[ y1 ][ x1 ] == 'X' )
				{
					continue;
				}
				REP( y2, 0, h )
				{
					REP( x2, 0, w )
					{
						if ( !( 0 <= y2 && y2 < h && 0 <= x2 && x2 < w ) || board[ y2 ][ x2 ] == 'X' )
						{
							continue;
						}

						if ( x1 != x2 && abs( x1 - x2 ) + abs( y1 - y2 ) <= 3 )
						{
							( x1 < x2 ? G[ y1 ][ x1 ][0] : G[ y1 ][ x1 ][1] ).PB( Edge( y2, x2, board[ y1 ][ x1 ] - '0' ) );
						}
					}
				}
			}
		}

		while ( !que.empty() )
		{
			int dist = que.top().fst;
			State cur = que.top().snd;
			que.pop();

			if ( distance[ cur.y ][ cur.x ][ cur.foot ] < dist )
			{
				continue;
			}

			FOR( e, G[ cur.y ][ cur.x ][ cur.foot ] )
			{
				if ( dist + e.cost < distance[ e.y ][ e.x ][ !( cur.foot ) ] )
				{
					distance[ e.y ][ e.x ][ !( cur.foot ) ] = dist + e.cost;
					que.push( MP( dist + e.cost, State( e.y, e.x, !( cur.foot ) ) ) );
				}
			}
		}

		int res = INF;
		FOR( goal, goals )
		{
			res = min( res, distance[ goal.fst ][ goal.snd ][0] );
			res = min( res, distance[ goal.fst ][ goal.snd ][1] );
		}

		cout << ( res == INF ? -1 : res ) << endl;
	}

	return 0;
}

Measured memory_bytes: 1876