#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <queue>
#include <limits>

using Bool = bool;
using Int = long long int;
using Char = char;
template <class... Ts>
using Tuple = std::tuple<Ts...>;
template <class T>
using Vector = std::vector<T>;
template <class T>
using Matrix = Vector<Vector<T>>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T>
T genv(T v) { return v; }

template <class T, class... Ts>
auto genv(size_t l, Ts... ts) {
    return Vector<decltype(genv<T>(ts...))>(l, genv<T>(ts...));
}

constexpr Int INF = std::numeric_limits<Int>::max() / 3;

Int dp[60][30][60][30][2];

Bool solve() {
    Int w, h;
    std::cin >> w >> h;
    if (w == 0) return false;

    Vector<Tuple<Int, Int>> start, goal;
    Matrix<Int> s = genv<Int>(h, w, 0LL);
    for (Int x = 0; x < h; ++x) {
        for (Int y = 0; y < w; ++y) {
            Char c;
            std::cin >> c;
            if (isdigit(c)) {
                s[x][y] = c - '0';
            } else if (c == 'S') {
                start.emplace_back(x, y);
            } else if (c == 'T') {
                goal.emplace_back(x, y);
            } else {
                s[x][y] = INF;
            }
        }
    }

    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) {
                    for (Int e = 0; e < 2; ++e) {
                        dp[lx][ly][rx][ry][e] = INF;
                    }
                }
            }
        }
    }

    MinHeap<Tuple<Int, Int, Int, Int, Int, Int>> heap;
    for (auto t : start) {
        Int x, y;
        std::tie(x, y) = t;
        for (Int e = 0; e < 2; ++e) {
            dp[x][y][x][y][e] = 0;
            heap.emplace(0, x, y, x, y, e);
        }
    }

    while (!heap.empty()) {
        Int d, lx, ly, rx, ry, e;
        std::tie(d, lx, ly, rx, ry, e) = heap.top();
        heap.pop();
        if (d > dp[lx][ly][rx][ry][e]) continue;

        for (Int x = 0; x < h; ++x) {
            for (Int y = 0; y < w; ++y) {
                Int nlx = lx, nly = ly, nrx = rx, nry = ry;
                if (e == 0) {
                    nrx = x, nry = y;
                } else {
                    nlx = x, nly = y;
                }

                if (nly >= nry ||
                    std::abs(nlx - nrx) + std::abs(nly - nry) > 3 ||
                    dp[nlx][nly][nrx][nry][1 - e] <= d + s[x][y]) {
                    continue;
                }

                dp[nlx][nly][nrx][nry][1 - e] = d + s[x][y];
                heap.emplace(dp[nlx][nly][nrx][nry][1 - e], nlx, nly, nrx, nry, 1 - e);
            }
        }
    }

    Int ans = INF;
    for (auto t : goal) {
        Int gx, gy;
        std::tie(gx, gy) = t;

        for (Int x = 0; x < h; ++x) {
            for (Int y = 0; y < w; ++y) {
                for (Int e = 0; e < 2; ++e) {
                    ans = std::min({ans, dp[x][y][gx][gy][e], dp[gx][gy][x][y][e]});
                }
            }
        }
    }

    std::cout << (ans >= INF ? -1 : ans) << std::endl;
    return true;
}

int main() {
    while (solve()) {}
    return 0;
}



Measured memory_bytes: 53756