/**
* author: mamion
* created: Saturday 2024-10-26
**/
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include<cpp-dump-main/cpp-dump.hpp>
#define debug(...) cpp_dump(__VA_ARGS__)
CPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);
CPP_DUMP_SET_OPTION_GLOBAL(log_label_func, cpp_dump::log_label::filename());
CPP_DUMP_SET_OPTION_GLOBAL(enable_asterisk, true);
#else
#define debug(...)
#endif // LOCAL
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int N = 2e5 + 100;
int m, n, d[N], ans;
char c[N];
inline int get(int x, int y) {
return (x - 1) * n + y;
}
void dijkstra(int state) {
memset(d, 0x3f, sizeof d);
priority_queue<pair<int, pair<int, int>>> pq;
if (state == 0)
for (int i = 1; i <= n; i++) {
int ij = get(1, i);
if ('0' <= c[ij] && c[ij] <= '9')
d[ij] = c[ij] - '0', pq.push({-d[ij], {1, i}});
}
if (state == 1)
for (int i = 1; i <= m; i++) {
int ij = get(i, 1);
if ('0' <= c[ij] && c[ij] <= '9')
d[ij] = c[ij] - '0', pq.push({-d[ij], {i, 1}});
}
while (pq.size()) {
int dist, u, v, uv;
dist = -pq.top().first; tie(u, v) = pq.top().second; pq.pop();
uv = get(u, v);
if (dist != d[uv]) continue;
for (int i = 0; i < 8; i++) {
int x = u + dx[i], y = v + dy[i];
if (x < 1 || m < x) continue;
if (y < 1 || n < y) continue;
int xy = get(x, y);
if (c[xy] < '0' || '9' < c[xy]) continue;
if (d[xy] > d[uv] + c[xy] - '0') {
d[xy] = d[uv] + c[xy] - '0';
pq.push({-d[xy], {x, y}});
}
}
}
if (state == 0) {
for (int i = 1; i <= n; i++) {
int ij = get(m, i);
ans = min(ans, d[ij]);
}
for (int i = 1; i <= m; i++) {
int ij = get(i, 1);
ans = min(ans, d[ij]);
}
}
if (state == 1) {
for (int i = 1; i <= n; i++) {
int ij = get(1, i);
ans = min(ans, d[ij]);
}
for (int i = 1; i <= m; i++) {
int ij = get(i, n);
ans = min(ans, d[ij]);
}
}
}
void solve() {
cin >> m >> n;
for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) {
int ij = get(i, j);
cin >> c[ij];
if (c[ij] == '#') c[ij] = '0';
}
ans = 1e9;
dijkstra(0);
dijkstra(1);
cout << (ans == 1e9 ? -1 : ans);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#else
#define file "name"
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
#endif // LOCAL
int T; T = 1; if (0) cin >> T;
for (int i = 1; i <= T; i++)
{
solve();
}
}
LyoqCiAqICAgIGF1dGhvcjogIG1hbWlvbgogKiAgICBjcmVhdGVkOiBTYXR1cmRheSAyMDI0LTEwLTI2CioqLwoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNpZmRlZiBMT0NBTAojaW5jbHVkZTxjcHAtZHVtcC1tYWluL2NwcC1kdW1wLmhwcD4KI2RlZmluZSBkZWJ1ZyguLi4pIGNwcF9kdW1wKF9fVkFfQVJHU19fKQpDUFBfRFVNUF9TRVRfT1BUSU9OX0dMT0JBTChtYXhfbGluZV93aWR0aCwgMTAwKTsKQ1BQX0RVTVBfU0VUX09QVElPTl9HTE9CQUwobG9nX2xhYmVsX2Z1bmMsIGNwcF9kdW1wOjpsb2dfbGFiZWw6OmZpbGVuYW1lKCkpOwpDUFBfRFVNUF9TRVRfT1BUSU9OX0dMT0JBTChlbmFibGVfYXN0ZXJpc2ssIHRydWUpOwojZWxzZQojZGVmaW5lIGRlYnVnKC4uLikKI2VuZGlmIC8vIExPQ0FMCgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgdWxsOwp0eXBlZGVmIGxvbmcgZG91YmxlIGxkOwoKaW50IGR4W10gPSB7LTEsIC0xLCAtMSwgMCwgMCwgMSwgMSwgMX07CmludCBkeVtdID0gey0xLCAwLCAxLCAtMSwgMSwgLTEsIDAsIDF9OwoKY29uc3QgaW50IE4gPSAyZTUgKyAxMDA7CmludCBtLCBuLCBkW05dLCBhbnM7CmNoYXIgY1tOXTsKCmlubGluZSBpbnQgZ2V0KGludCB4LCBpbnQgeSkgewogICAgcmV0dXJuICh4IC0gMSkgKiBuICsgeTsKfQoKdm9pZCBkaWprc3RyYShpbnQgc3RhdGUpIHsKICAgIG1lbXNldChkLCAweDNmLCBzaXplb2YgZCk7CiAgICBwcmlvcml0eV9xdWV1ZTxwYWlyPGludCwgcGFpcjxpbnQsIGludD4+PiBwcTsKCiAgICBpZiAoc3RhdGUgPT0gMCkKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICAgICAgaW50IGlqID0gZ2V0KDEsIGkpOwogICAgICAgICAgICBpZiAoJzAnIDw9IGNbaWpdICYmIGNbaWpdIDw9ICc5JykKICAgICAgICAgICAgICAgIGRbaWpdID0gY1tpal0gLSAnMCcsIHBxLnB1c2goey1kW2lqXSwgezEsIGl9fSk7CiAgICAgICAgfQoKICAgIGlmIChzdGF0ZSA9PSAxKQogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG07IGkrKykgewogICAgICAgICAgICBpbnQgaWogPSBnZXQoaSwgMSk7CiAgICAgICAgICAgIGlmICgnMCcgPD0gY1tpal0gJiYgY1tpal0gPD0gJzknKQogICAgICAgICAgICAgICAgZFtpal0gPSBjW2lqXSAtICcwJywgcHEucHVzaCh7LWRbaWpdLCB7aSwgMX19KTsKICAgICAgICB9CgogICAgd2hpbGUgKHBxLnNpemUoKSkgewogICAgICAgIGludCBkaXN0LCB1LCB2LCB1djsKICAgICAgICBkaXN0ID0gLXBxLnRvcCgpLmZpcnN0OyB0aWUodSwgdikgPSBwcS50b3AoKS5zZWNvbmQ7IHBxLnBvcCgpOwogICAgICAgIHV2ID0gZ2V0KHUsIHYpOwogICAgICAgIGlmIChkaXN0ICE9IGRbdXZdKSBjb250aW51ZTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IDg7IGkrKykgewogICAgICAgICAgICBpbnQgeCA9IHUgKyBkeFtpXSwgeSA9IHYgKyBkeVtpXTsKICAgICAgICAgICAgaWYgKHggPCAxIHx8IG0gPCB4KSBjb250aW51ZTsKICAgICAgICAgICAgaWYgKHkgPCAxIHx8IG4gPCB5KSBjb250aW51ZTsKICAgICAgICAgICAgaW50IHh5ID0gZ2V0KHgsIHkpOwogICAgICAgICAgICBpZiAoY1t4eV0gPCAnMCcgfHwgJzknIDwgY1t4eV0pIGNvbnRpbnVlOwogICAgICAgICAgICBpZiAoZFt4eV0gPiBkW3V2XSArIGNbeHldIC0gJzAnKSB7CiAgICAgICAgICAgICAgICBkW3h5XSA9IGRbdXZdICsgY1t4eV0gLSAnMCc7CiAgICAgICAgICAgICAgICBwcS5wdXNoKHstZFt4eV0sIHt4LCB5fX0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIGlmIChzdGF0ZSA9PSAwKSB7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgICAgIGludCBpaiA9IGdldChtLCBpKTsKICAgICAgICAgICAgYW5zID0gbWluKGFucywgZFtpal0pOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBtOyBpKyspIHsKICAgICAgICAgICAgaW50IGlqID0gZ2V0KGksIDEpOwogICAgICAgICAgICBhbnMgPSBtaW4oYW5zLCBkW2lqXSk7CiAgICAgICAgfQogICAgfQogICAgaWYgKHN0YXRlID09IDEpIHsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICAgICAgaW50IGlq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