#include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>
   
using namespace std;

bool ischecked[61][61][2];
char cliff[61][61];
int tesu[61][61][2];
int w, h;

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

typedef struct Data
{
    int x;
    int y;
    int isnowleft;
} d;

int solve(int isleft, int sx, int sy)
{
    int ans = 1000000;
    queue<d> qu;
    d tmp;
    tmp.x = sx;
    tmp.y = sy;
    tmp.isnowleft = isleft;
    qu.push(tmp);
    while(!qu.empty()){
        d top = qu.front();
        qu.pop();
        int x = top.x;
        int y = top.y;
        int isnowleft = top.isnowleft;
        // cout << x << " " << y << " " << tesu[x][y][isnowleft] << " " << isnowleft << endl;
        for(int i = 0; i < 9; i++){
            int nx = x + dx[i];
            int ny = y + dy[isnowleft][i];
            if(nx < 0 || ny < 0 || nx >= h || ny >= w || cliff[nx][ny] == 'X') continue;
            if(cliff[nx][ny] == 'T') ans = min(tesu[x][y][isnowleft], ans);
            // else if(cliff[nx][ny] == 'S') solve(!isnowleft, nx, ny, nowtesu);
            else{
                d next;
                next.x = nx;
                next.y = ny;
                next.isnowleft = !isnowleft;
                int nexttesu;
                if(cliff[nx][ny] == 'S') nexttesu = tesu[x][y][isnowleft];
                else nexttesu = tesu[x][y][isnowleft] + cliff[nx][ny] - '0';
                if(nexttesu < tesu[nx][ny][!isnowleft]){
                    tesu[nx][ny][!isnowleft] = nexttesu;
                    // ischecked[nx][ny][!isnowleft] = true;
                    qu.push(next);
                }
            }
        }
    }
    return ans;
}

int main()
{
    while(1){
        cin >> w >> h;
        vector<int> sx, sy, tx, ty;
        if(w == 0 && h == 0) break;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> cliff[i][j];
                if(cliff[i][j] == 'S'){
                    sx.push_back(i);
                    sy.push_back(j);
                } else if(cliff[i][j] == 'T'){
                    tx.push_back(i);
                    ty.push_back(j);
                }
            }
        }
        // memset(ischecked, false, sizeof(ischecked));
        memset(tesu, 1000000, sizeof(tesu));
        int ans = 1000000;
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < sx.size(); j++){
                tesu[sx[j]][sy[j]][i] = 0;
                // ischecked[sx[j]][sy[j]][i] = true;
                ans = min(ans, solve(i, sx[j], sy[j]));
            }
        }
        // for(int k = 0; k < 2; k++){
        //     for(int i = 0; i < h; i++){
        //             for(int j = 0; j < w; j++){
        //                 cout << tesu[i][j][k] << " ";
        //             }
        //             cout << endl;
        //         }
        //         cout << endl;
        //     }
        if(ans == 1000000) cout << -1 << endl;
        else cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 1272