#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

struct State
{
    int lx, ly, rx, ry, last, cost;
};

bool operator<(const State &e, const State &f)
{
    return e.cost > f.cost;
}

int dp[70][40][70][40][2];
int c[70][40];

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

    int w, h;
    while (cin >> w >> h, w || h)
    {
        vector<P> st, gt;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                string s;
                cin >> s;
                if (s == "X")
                    c[i][j] = INF;
                else if (s == "S")
                {
                    c[i][j] = 0;
                    st.push_back(P(i, j));
                }
                else if (s == "T")
                {
                    c[i][j] = 0;
                    gt.push_back(P(i, j));
                }
                else
                {
                    c[i][j] = stoi(s);
                }
            }
        }
        for (int lx = 0; lx < h; lx++)
            for (int ly = 0; ly < w; ly++)
                for (int rx = 0; rx < h; rx++)
                    for (int ry = 0; ry < w; ry++)
                    {
                        dp[lx][ly][rx][ry][0] = INF;
                        dp[lx][ly][rx][ry][1] = INF;
                    }
        priority_queue<State> q;
        for (auto p : st)
        {
            int x = p.first, y = p.second;
            dp[x][y][h][w][0] = 0;
            dp[h][w][x][y][1] = 0;
            q.push(State{x, y, h, w, 0, 0});
            q.push(State{h, w, x, y, 1, 0});
        }
        while (!q.empty())
        {
            State st = q.top();
            q.pop();
            int lx = st.lx, ly = st.ly, rx = st.rx, ry = st.ry, cost = st.cost;
            if (st.last == 0)
            {
                for (int i = 0; i < h; i++)
                {
                    for (int j = ly + 1; j < w; j++)
                    {
                        if (c[i][j] == INF)
                            continue;
                        if (abs(lx - i) + abs(ly - j) > 3)
                            continue;
                        if (dp[lx][ly][i][j][1] != INF)
                            continue;
                        dp[lx][ly][i][j][1] = cost + c[i][j];
                        q.push(State{lx, ly, i, j, 1, cost + c[i][j]});
                    }
                }
            }
            else
            {
                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < ry; j++)
                    {
                        if (c[i][j] == INF)
                            continue;
                        if (abs(i - rx) + abs(j - ry) > 3)
                            continue;
                        if (dp[i][j][rx][ry][0] != INF)
                            continue;
                        dp[i][j][rx][ry][0] = cost + c[i][j];
                        q.push(State{i, j, rx, ry, 0, cost + c[i][j]});
                    }
                }
            }
        }
        int res = INF;
        for (auto g : gt)
        {
            int lx = g.first, ly = g.second;
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    res = min(res, dp[lx][ly][i][j][0]);
                }
            }
        }
        for (auto g : gt)
        {
            int rx = g.first, ry = g.second;
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    res = min(res, dp[i][j][rx][ry][1]);
                }
            }
        }
        if (res == INF)
            cout << -1 << endl;
        else
            cout << res << endl;
    }
}



Measured memory_bytes: 42768