#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

struct edge {
    int to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void addedge(int st, int ed, int co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G[st].push_back(e);
}

void dijkstra(int s) {
    priority_queue<plglg, vector<plglg>, greater<plglg> > que;
    fill(d, d + V + 10, llmax);
    d[s] = 0;
    que.push(plglg(0, s));

    while (!que.empty()) {
        plglg p = que.top();
        que.pop();
        ll v = p.second;
        if (d[v] < p.first) {
            continue;
        }
        int len = G[v].size();
        for (int i = 0; i < len; i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(plglg(d[e.to], e.to));
            }
        }
    }
}

int bo[70][70];

ll calcost(int i) {
    if (i >= 0) {
        return i;
    } else if (i == -1) {
        return llmax;
    } else {
        return 0;
    }
}

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

int main() {
    while (1) {
        int w, h;
        cin >> w >> h;
        if (w == 0) {
            break;
        }
        int num = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                char c;
                cin >> c;
                if (c == 'X') {
                    bo[i][j] = -1;
                } else if (isdigit(c)) {
                    bo[i][j] = c - '0';
                } else if (c == 'S') {
                    bo[i][j] = -2;
                    num++;
                } else {
                    bo[i][j] = -3;
                }
            }
        }
        V = (w * h) * 9 * 2 + (w * h) * 2 + 2;
        for (int i = 0; i < V; i++) {
            G[i].clear();
        }
        for (int i1 = 0; i1 < h; i1++) {
            for (int j1 = 0; j1 < w; j1++) {
                for (int k = 0; k < 9; k++) {
                    int i2 = i1 + foot[k][0];
                    int j2 = j1 - foot[k][1];
                    if (i2 >= 0 && i2 < h && j2 >= 0 && j2 < w) {
                        if (bo[i2][j2] != -1) {
                            for (int k2 = 0; k2 < 9; k2++) {
                                int x = i2 + foot[k2][0];
                                int y = j2 + foot[k2][1];
                                if (x >= 0 && x < h && y >= 0 && y < w) {
                                    if (bo[x][y] != -1) {
                                        addedge(((i1 * w + j1) * 9 + k) * 2 + 1, ((i2 * w + j2) * 9 + k2) * 2,
                                                calcost(bo[x][y]));
                                    }
                                }
                            }
                        }
                    }
                }

                for (int k = 0; k < 9; k++) {
                    int i2 = i1 + foot[k][0];
                    int j2 = j1 + foot[k][1];
                    if (i2 >= 0 && i2 < h && j2 >= 0 && j2 < w) {
                        if (bo[i2][j2] != -1) {
                            for (int k2 = 0; k2 < 9; k2++) {
                                int x = i2 + foot[k2][0];
                                int y = j2 - foot[k2][1];
                                if (x >= 0 && x < h && y >= 0 && y < w) {
                                    if (bo[x][y] != -1) {
                                        addedge(((i1 * w + j1) * 9 + k) * 2, ((i2 * w + j2) * 9 + k2) * 2 + 1,
                                                calcost(bo[x][y]));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (bo[i][j] == -2) {
                    addedge(V - 2, (w * h) * 9 * 2 + (i * w + j) * 2 + 1, 0);
                    for (int k = 0; k < 9; k++) {
                        int x = i + foot[k][0];
                        int y = j + foot[k][1];
                        if (x >= 0 && x < h && y >= 0 && y < w) {
                            if (bo[x][y] != -1) {
                                addedge((w * h) * 9 * 2 + (i * w + j) * 2 + 1,
                                        (i * w + j) * 9 * 2 + k * 2, calcost(bo[x][y]));
                            }
                        }
                    }
                    addedge(V - 2, (w * h) * 9 * 2 + (i * w + j) * 2, 0);
                    for (int k = 0; k < 9; k++) {
                        int x = i + foot[k][0];
                        int y = j - foot[k][1];
                        if (x >= 0 && x < h && y >= 0 && y < w) {
                            if (bo[x][y] != -1) {
                                addedge((w * h) * 9 * 2 + (i * w + j) * 2,
                                        (i * w + j) * 9 * 2 + k * 2 + 1, calcost(bo[x][y]));
                            }
                        }
                    }
                }
                for (int k = 0; k < 9; k++) {
                    int x = i + foot[k][0];
                    int y = j + foot[k][1];
                    if (x >= 0 && x < h && y >= 0 && y < w) {
                        if (bo[x][y] == -3) {
                            addedge((i * w + j) * 9 * 2 + k * 2, V - 1, 0);
                        }
                    }
                    int x2 = i + foot[k][0];
                    int y2 = j - foot[k][1];
                    if (x2 >= 0 && x2 < h && y2 >= 0 && y2 < w) {
                        if (bo[x2][y2] == -3) {
                            addedge((i * w + j) * 9 * 2 + k * 2 + 1, V - 1, 0);
                        }
                    }
                }
            }
        }
        dijkstra(V - 2);
        ll ans = d[V - 1];
        if (ans != llmax) {
            cout << ans << endl;
        } else {
            cout << -1 << endl;
        }
    }
}



Measured memory_bytes: 31424