#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<(n);i++)
#define loop(i,a,n) for(i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int v[60][30];

int dx[] = {-2,-1,-1,0,0,0,1,1,2};
int dy[] = {1,1,2,1,2,3,1,2,1};//反転

int h,w;

bool in(int x, int y){
    if(x < 0 || x >= h) return false;
    if(y < 0 || y >= w) return false;
    return true;
}

int main(void) {
    int i,j,k;
    while(cin >> w >> h, w){

        vector<pii> s,g;

        rep(i,h)rep(j,w){
            string t;
            cin >> t;

            if(isdigit(t[0])){
                v[i][j] = t[0] - '0';
            }else if(t[0] == 'S'){
                v[i][j] = 0;
                s.push_back(pii(i,j));
            }else if(t[0] == 'T'){
                v[i][j] = 0;
                g.push_back(pii(i,j));
            }else{
                v[i][j] = -1;
            }

        }

        int dp[60][30][2];
        rep(i,h)rep(j,w)rep(k,2) dp[i][j][k] = INF;

        queue<vi> q;

        rep(i,s.size())rep(j,2){
            vi t(3);
            t[0] = s[i].first;
            t[1] = s[i].second;
            t[2] = j;
            dp[t[0]][t[1]][j] = 0;
            q.push(t);
        }

        while(q.size()){
            int x = q.front()[0];
            int y = q.front()[1];
            int z = q.front()[2];
            q.pop();
            //cout << x << " " << y << " " << z << endl;
            rep(i,9){
                int nx = x + dx[i];
                int ny = y + dy[i];
                int nz = 1;
                if(z){
                    ny = y - dy[i];
                    nz = 0;
                }
                if(!in(nx,ny) || v[nx][ny] < 0)continue;
                vi t(3);
                t[0] = nx;
                t[1] = ny;
                t[2] = nz;
                if(dp[nx][ny][nz] > dp[x][y][z] + v[x][y]){
                    dp[nx][ny][nz] = dp[x][y][z] + v[x][y];
                    q.push(t);
                }
            }
        }



        int ans = INF;
        rep(i,g.size())rep(j,2){
            ans = min(ans,dp[g[i].first][g[i].second][j]);
        }
        if(ans < INF){
            cout << ans << endl;
        }else{
            cout << -1 << endl;
        }
    }
}

