#include <iostream>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int W,H;
int tiles[31][61] = {};
int tleft[31][61] = {};
int tright[31][61] = {};
struct Position {
    int x,y;
    bool foot; // true:left false:right
};
struct CompareByFirst {
    bool operator()(pair<int, Position> const & a,
                              pair<int, Position> const & b) const
    { return a.first > b.first; }
};
vector<int> answers;

int main() {
START:
    while (cin>>W>>H && W != 0 && H != 0) {
        priority_queue<pair<int, Position>, vector<pair<int, Position> >, CompareByFirst> pqueue;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                tleft[w][h] = -1;
                tright[w][h] = -1;
                char tile = 0;
                cin>>tile;
                switch(tile) {
                    case 'S':
                    {
                        tiles[w][h] = 0;
                        Position pos = {w,h,true};
                        pqueue.push(make_pair(0, pos));
                        pos.foot = false;
                        pqueue.push(make_pair(0, pos));
                        break;
                    }

                    case 'T':
                    {
                        tiles[w][h] = -2;
                        break;
                    }

                    case 'X':
                        tiles[w][h] = -1;
                        break;

                    default:
                        tiles[w][h] = atoi(&tile);
                }
            }
        }

        while (!pqueue.empty()) {
            pair<int, Position> p = pqueue.top();
            pqueue.pop();
            if (p.second.foot == true) {
                if (tleft[p.second.x][p.second.y] == -1) tleft[p.second.x][p.second.y] = p.first;
                else continue;

                for (int h = -2; h <= 2; h++) {
                    for (int w = 1; w <= 3 - abs(h); w++) {
                        if (0 <= p.second.x+w && p.second.x+w < W && 0 <= p.second.y+h && p.second.y+h < H) {
                            if (tiles[p.second.x+w][p.second.y+h] == -1) continue;
                            else if (tiles[p.second.x+w][p.second.y+h] == -2) {
                                answers.push_back(p.first);
                                goto START;
                            } else {
                                Position pos = {p.second.x+w, p.second.y+h, false};
                                //cout<<"distance:"<<p.first+tiles[p.second.x+w][p.second.y+h]<<", x:"<<pos.x<<", y:"<<pos.y<<", foot:"<<(pos.foot ? "left" : "right")<<endl;
                                pqueue.push(make_pair(p.first+tiles[p.second.x+w][p.second.y+h], pos));
                            }
                        }
                    }
                }
            } else {
                if (tright[p.second.x][p.second.y] == -1) tright[p.second.x][p.second.y] = p.first;
                else continue;

                for (int h = -2; h <= 2; h++) {
                    for (int w = -1; w >= abs(h) - 3; w--) {
                        if (0 <= p.second.x+w && p.second.x+w < W && 0 <= p.second.y+h && p.second.y+h < H) {
                            if (tiles[p.second.x+w][p.second.y+h] == -1) continue;
                            else if (tiles[p.second.x+w][p.second.y+h] == -2) {
                                answers.push_back(p.first);
                                goto START;
                            } else {
                                Position pos = {p.second.x+w, p.second.y+h, true};
                                //cout<<"distance:"<<p.first+tiles[p.second.x+w][p.second.y+h]<<", x:"<<pos.x<<", y:"<<pos.y<<", foot:"<<(pos.foot ? "left" : "right")<<endl;
                                pqueue.push(make_pair(p.first+tiles[p.second.x+w][p.second.y+h], pos));
                            }
                        }
                    }
                }
            }
        }

        answers.push_back(-1);
    }

    for (int i = 0; i < answers.size(); i++) cout<<answers[i]<<endl;
}

Measured memory_bytes: 3208