#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned int
#define test int t;cin>>t; while(t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int>     pii;
typedef pair<char, int>    pci;
typedef pair<char, char>   pcc;
typedef vector<pii>       vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(x) (int)x.size()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007

void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
// You should only debug a pair of simple data types. For example,
// the debug won't work if one of pair's elements is collection type
// (std::vector, std::map, std::set...).
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

// bool check_prime(int n)  return whether a number is prime or not O(sqrt(N));
// int BE(int x,int n,int m)  return x^n%m; O(logN);
// void sieve()  from number 1-1000001 saare prime store kar lega  in bool is_prime[1000001] array
// vector<int> z_array(string s)  return vector which is Z-array of string s;
// vector<int> lps(string s)      return vector which is lps array of string s;
// int power(int x,int n) return x^n; O(logN);
// using_ordered_set (template for using ordered set , replace first 2 lines of this page with this code..);
const int gf = 1e6 + 9;
char s[1009][1009];
int dr[] = { -1, 1, 0, 0};
int dc[] = {0, 0, 1, -1};
int dis[1009][1009];
int vis[1009][1009];

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	if (fopen("input.txt", "r"))
	{
		freopen ("input.txt" , "r" , stdin);
		freopen ("output.txt" , "w" , stdout);
	}
#endif
	//_______________________________-code starts-_______________________________________________

	int n, m;
	cin >> n >> m;
	char x1, y1;
	int e1, e2;

	zip(i, 0, n)
	{
		zip(j, 0, m)
		{
			char x;
			cin >> x;
			s[i][j] = x;
			if (x == 'S')
			{
				x1 = i, y1 = j;
			}
			if (x == 'E')
			{
				e1 = i, e2 = j;
			}
		}
	}

	queue<pii>q;
	q.push({e1, e2});
	int x, y;
	vis[e1][e2] = true;
	// zip(i, 0, n)
	// {
	// 	zip(j, 0, m)
	// 	cout << s[i][j] << " ";
	// 	cout << en;
	// }
	while (sz(q))
	{
		pii p = q.front();
		debug(p);
		q.pop();
		x = p.ff;
		y = p.ss;

		if (s[p.ff][p.ss] == 'T')
		{
			continue;
		}
		//cout << "oen" << endl;
		zip(i, 0, 4)
		{
			x = p.ff + dr[i];
			y = p.ss + dc[i];
			debug(x, y);
			if (x < 0 || y < 0 || x >= n || y >= m)
				continue;
			if (vis[x][y] || s[x][y] == 'T')
				continue;
			debug(x, y);
			pii p2 = {x, y};
			q.push(p2);
			vis[x][y] = true;
			dis[x][y] = dis[p.ff][p.ss] + 1;
		}
	}
	int ans = 0;
	// cout << dis[x1][y1] << endl;
	// zip(i, 0, n)
	// {
	// 	zip(j, 0, m)
	// 	cout << dis[i][j] << " ";
	// 	cout << en;
	// }
	zip(i, 0, n)
	{
		zip(j, 0, m)
		{
			int d1 = dis[i][j];
			int d2 = dis[x1][y1];
			if (d2 > 0 && d1 > 0 && d1 <= d2 && s[i][j] != 'E' && s[i][j] != 'S' && s[i][j] != 'T')
			{
				ans += (s[i][j] - '0');
			}
		}
	}
	cout << ans << en;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned int
#define test int t;cin>>t; while(t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int>     pii;
typedef pair<char, int>    pci;
typedef pair<char, char>   pcc;
typedef vector<pii>       vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(x) (int)x.size()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007

void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
// You should only debug a pair of simple data types. For example,
// the debug won't work if one of pair's elements is collection type
// (std::vector, std::map, std::set...).
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

// bool check_prime(int n)  return whether a number is prime or not O(sqrt(N));
// int BE(int x,int n,int m)  return x^n%m; O(logN);
// void sieve()  from number 1-1000001 saare prime store kar lega  in bool is_prime[1000001] array
// vector<int> z_array(string s)  return vector which is Z-array of string s;
// vector<int> lps(string s)      return vector which is lps array of string s;
// int power(int x,int n) return x^n; O(logN);
// using_ordered_set (template for using ordered set , replace first 2 lines of this page with this code..);
const int gf = 1e6 + 9;
char s[1009][1009];
int dr[] = { -1, 1, 0, 0};
int dc[] = {0, 0, 1, -1};
int dis[1009][1009];
int vis[1009][1009];

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	if (fopen("input.txt", "r"))
	{
		freopen ("input.txt" , "r" , stdin);
		freopen ("output.txt" , "w" , stdout);
	}
#endif
	//_______________________________-code starts-_______________________________________________

	int n, m;
	cin >> n >> m;
	int x1, y1;
	int e1, e2;

	zip(i, 0, n)
	{
		zip(j, 0, m)
		{
			char x;
			cin >> x;
			s[i][j] = x;
			if (x == 'S')
			{
				x1 = i, y1 = j;
			}
			if (x == 'E')
			{
				e1 = i, e2 = j;
			}
		}
	}
	memset(dis, -1, sizeof(dis));

	queue<pii>q;
	q.push({e1, e2});
	int x, y;
	vis[e1][e2] = true;
	dis[e1][e2] = 0;
	// zip(i, 0, n)
	// {
	// 	zip(j, 0, m)
	// 	cout << s[i][j] << " ";
	// 	cout << en;
	// }
	while (sz(q))
	{
		pii p = q.front();
		debug(p);
		q.pop();
		x = p.ff;
		y = p.ss;

		if (s[p.ff][p.ss] == 'T')
		{
			continue;
		}
		//cout << "oen" << endl;
		zip(i, 0, 4)
		{
			x = p.ff + dr[i];
			y = p.ss + dc[i];
			debug(x, y);
			if (x < 0 || y < 0 || x >= n || y >= m)
				continue;
			if (vis[x][y] || s[x][y] == 'T')
				continue;
			debug(x, y);
			pii p2 = {x, y};
			q.push(p2);
			vis[x][y] = true;
			dis[x][y] = dis[p.ff][p.ss] + 1;
		}
	}
	int ans = 0;
	// cout << dis[x1][y1] << endl;
	// zip(i, 0, n)
	// {
	// 	zip(j, 0, m)
	// 	cout << dis[i][j] << " ";
	// 	cout << en;
	// }
	zip(i, 0, n)
	{
		zip(j, 0, m)
		{
			int d1 = dis[i][j];
			//int d2 = dis[x1][y1];
			if (d1 != -1 && d1 != -1 && d1 <=  dis[x1][y1] && s[i][j] != 'E' && s[i][j] != 'S' && s[i][j] != 'T')
			{
				ans += (s[i][j] - '0');
			}
		}
	}
	cout << ans << en;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <iterator>
#include <fstream>
#include <math.h>

#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <bitset>

#include <random>
#include <ctime>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;

#define FOR(i, from, to) for (int i = from; i < to; i++)
#define ROF(i, from, to) for (int i = from; i > to; i--)

const double PI = 3.141592653589793238463;
const int INF = 0x3f3f3f3f;
const int INFS = 1000000000;
const ll LLINF = 9223372036854775807;
const int M = 1000000007;
const int HM = 2147483647;
const int HP = 43;
const double EPS = 1e-8;

int a[1000][1000], ans[1000][1000];
bool used[1000][1000];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    FOR(i, 0, 1000) {
        FOR(j, 0, 1000) {
            ans[i][j] = INF;
        }
    }
    int n, m, sti, stj, endi, endj;
    char t;
    cin >> n >> m;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            cin >> t;
            if (t == 'S') {
                a[i][j] = -3;
                sti = i;
                stj = j;
            } else if (t == 'E') {
                a[i][j] = -2;
                endi = i;
                endj = j;
            } else if (t == 'T')
                a[i][j] = -1;
            else
                a[i][j] = t - '0';
        }
    }
    queue<pair<pair<int, int>, int> > q;
    q.push({{ endi, endj }, 0 });
    while (!q.empty()) {
        int i = q.front().first.first;
        int j = q.front().first.second;
        int curr = q.front().second;
        q.pop();
        ++curr;
        used[i][j] = true;
        ans[i][j] = curr;
        if (i > 0 && a[i - 1][j] != -1 && !used[i - 1][j])
            q.push({ { i - 1, j }, curr });
        if (i < n - 1 && a[i + 1][j] != -1 && !used[i + 1][j])
            q.push({ { i + 1, j }, curr });
        if (j > 0 && a[i][j - 1] != -1 && !used[i][j - 1])
            q.push({ { i, j - 1 }, curr });
        if (j < m - 1 && a[i][j + 1] != -1 && !used[i][j + 1])
            q.push({ { i, j + 1 }, curr });
    }
    int res = 0;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            if (a[i][j] > 0 && ans[i][j] <= ans[sti][stj])
                res += a[i][j];
        }
    }
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <iterator>
#include <fstream>
#include <math.h>

#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <bitset>

#include <random>
#include <ctime>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;

#define FOR(i, from, to) for (int i = from; i < to; i++)
#define ROF(i, from, to) for (int i = from; i > to; i--)

const double PI = 3.141592653589793238463;
const int INF = 0x3f3f3f3f;
const int INFS = 1000000000;
const ll LLINF = 9223372036854775807;
const int M = 1000000007;
const int HM = 2147483647;
const int HP = 43;
const double EPS = 1e-8;

#define int long long

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, m, sti, stj, endi, endj;
    char t;
    cin >> n >> m;
    vector<vector<int> > a(n, vector<int>(m)), ans(n, vector<int>(m, -1));
    vector<vector<bool> > used(n, vector<bool>(m, false));
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            cin >> t;
            if (t == 'S') {
                a[i][j] = -3;
                sti = i;
                stj = j;
            } else if (t == 'E') {
                a[i][j] = -2;
                endi = i;
                endj = j;
            } else if (t == 'T')
                a[i][j] = -1;
            else
                a[i][j] = t - '0';
        }
    }
    queue<pair<pair<int, int>, int> > q;
    q.push({{ endi, endj }, 0 });
    while (!q.empty()) {
        int i = q.front().first.first;
        int j = q.front().first.second;
        int curr = q.front().second;
        q.pop();
        ++curr;
        used[i][j] = true;
        ans[i][j] = curr;
        if (i > 0 && a[i - 1][j] != -1 && !used[i - 1][j])
            q.push({ { i - 1, j }, curr });
        if (i < n - 1 && a[i + 1][j] != -1 && !used[i + 1][j])
            q.push({ { i + 1, j }, curr });
        if (j > 0 && a[i][j - 1] != -1 && !used[i][j - 1])
            q.push({ { i, j - 1 }, curr });
        if (j < m - 1 && a[i][j + 1] != -1 && !used[i][j + 1])
            q.push({ { i, j + 1 }, curr });
    }
    int res = 0;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            if (a[i][j] > 0 && ans[i][j] <= ans[sti][stj])
                res += a[i][j];
        }
    }
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <iterator>
#include <fstream>
#include <math.h>

#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <bitset>

#include <random>
#include <ctime>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;

#define FOR(i, from, to) for (int i = from; i < to; i++)
#define ROF(i, from, to) for (int i = from; i > to; i--)

const double PI = 3.141592653589793238463;
const int INF = 0x3f3f3f3f;
const int INFS = 1000000000;
const ll LLINF = 9223372036854775807;
const int M = 1000000007;
const int HM = 2147483647;
const int HP = 43;
const double EPS = 1e-8;

#define int long long

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, m, sti, stj, endi, endj;
    char t;
    cin >> n >> m;
    vector<vector<int> > a(n, vector<int>(m)), ans(n, vector<int>(m, INF));
    vector<vector<bool> > used(n, vector<bool>(m, false));
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            cin >> t;
            if (t == 'S') {
                a[i][j] = -3;
                sti = i;
                stj = j;
            } else if (t == 'E') {
                a[i][j] = -2;
                endi = i;
                endj = j;
            } else if (t == 'T')
                a[i][j] = -1;
            else
                a[i][j] = t - '0';
        }
    }
    queue<pair<pair<int, int>, int> > q;
    q.push({{ endi, endj }, 0 });
    while (!q.empty()) {
        int i = q.front().first.first;
        int j = q.front().first.second;
        int curr = q.front().second;
        q.pop();
        ++curr;
        used[i][j] = true;
        ans[i][j] = curr;
        if (i > 0 && a[i - 1][j] != -1 && !used[i - 1][j])
            q.push({ { i - 1, j }, curr });
        if (i < n - 1 && a[i + 1][j] != -1 && !used[i + 1][j])
            q.push({ { i + 1, j }, curr });
        if (j > 0 && a[i][j - 1] != -1 && !used[i][j - 1])
            q.push({ { i, j - 1 }, curr });
        if (j < m - 1 && a[i][j + 1] != -1 && !used[i][j + 1])
            q.push({ { i, j + 1 }, curr });
    }
    int res = 0;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            if (a[i][j] > 0 && ans[i][j] <= ans[sti][stj])
                res += a[i][j];
        }
    }
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <iterator>
#include <fstream>
#include <math.h>

#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <bitset>

#include <random>
#include <ctime>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;

#define FOR(i, from, to) for (int i = from; i < to; i++)
#define ROF(i, from, to) for (int i = from; i > to; i--)

const double PI = 3.141592653589793238463;
const int INF = 0x3f3f3f3f;
const int INFS = 1000000000;
const ll LLINF = 9223372036854775807;
const int M = 1000000007;
const int HM = 2147483647;
const int HP = 43;
const double EPS = 1e-8;

int a[1000][1000], ans[1000][1000];
bool used[1000][1000];
queue<pair<pair<int, int>, int> > q;

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    FOR(i, 0, 1000) {
        FOR(j, 0, 1000) {
            ans[i][j] = INF;
        }
    }
    int n, m, sti, stj, endi, endj;
    char t;
    cin >> n >> m;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            cin >> t;
            if (t == 'S') {
                a[i][j] = -3;
                sti = i;
                stj = j;
            } else if (t == 'E') {
                a[i][j] = -2;
                endi = i;
                endj = j;
            } else if (t == 'T')
                a[i][j] = -1;
            else
                a[i][j] = t - '0';
        }
    }
    q.push({{ endi, endj }, 0 });
    used[endi][endj] = true;
    int i, j, curr;
    while (!q.empty()) {
        i = q.front().first.first;
        j = q.front().first.second;
        curr = q.front().second;
        q.pop();
        ++curr;
        ans[i][j] = curr;
        if (i > 0 && a[i - 1][j] != -1 && !used[i - 1][j]) {
            used[i - 1][j] = true;
            q.push({{i - 1, j}, curr});
        }
        if (i < n - 1 && a[i + 1][j] != -1 && !used[i + 1][j]) {
            used[i + 1][j] = true;
            q.push({{i + 1, j}, curr});
        }
        if (j > 0 && a[i][j - 1] != -1 && !used[i][j - 1]) {
            used[i][j - 1] = true;
            q.push({{i, j - 1}, curr});
        }
        if (j < m - 1 && a[i][j + 1] != -1 && !used[i][j + 1]) {
            used[i][j + 1] = true;
            q.push({{i, j + 1}, curr});
        }
    }
    int res = 0;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            if (a[i][j] > 0 && ans[i][j] <= ans[sti][stj])
                res += a[i][j];
        }
    }
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <iterator>
#include <fstream>
#include <math.h>

#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <bitset>

#include <random>
#include <ctime>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;

#define FOR(i, from, to) for (int i = from; i < to; i++)
#define ROF(i, from, to) for (int i = from; i > to; i--)

const double PI = 3.141592653589793238463;
const int INF = 0x3f3f3f3f;
const int INFS = 1000000000;
const ll LLINF = 9223372036854775807;
const int M = 1000000007;
const int HM = 2147483647;
const int HP = 43;
const double EPS = 1e-8;

int a[1000][1000], ans[1000][1000];
bool used[1000][1000];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    FOR(i, 0, 1000) {
        FOR(j, 0, 1000) {
            ans[i][j] = INF;
        }
    }
    int n, m, sti, stj, endi, endj;
    char t;
    cin >> n >> m;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            cin >> t;
            if (t == 'S') {
                a[i][j] = -3;
                sti = i;
                stj = j;
            } else if (t == 'E') {
                a[i][j] = -2;
                endi = i;
                endj = j;
            } else if (t == 'T')
                a[i][j] = -1;
            else
                a[i][j] = t - '0';
        }
    }
    queue<pair<pair<int, int>, int> > q;
    q.push({{ endi, endj }, 0 });
    int i, j, curr;
    while (!q.empty()) {
        i = q.front().first.first;
        j = q.front().first.second;
        curr = q.front().second;
        q.pop();
        ++curr;
        used[i][j] = true;
        ans[i][j] = curr;
        if (i > 0 && a[i - 1][j] != -1 && !used[i - 1][j])
            q.push({ { i - 1, j }, curr });
        if (i < n - 1 && a[i + 1][j] != -1 && !used[i + 1][j])
            q.push({ { i + 1, j }, curr });
        if (j > 0 && a[i][j - 1] != -1 && !used[i][j - 1])
            q.push({ { i, j - 1 }, curr });
        if (j < m - 1 && a[i][j + 1] != -1 && !used[i][j + 1])
            q.push({ { i, j + 1 }, curr });
    }
    int res = 0;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            if (a[i][j] > 0 && ans[i][j] <= ans[sti][stj])
                res += a[i][j];
        }
    }
    cout << res << endl;

    return 0;
}


//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Allah Buyuktur.
//Bismillahirrahmanirrahim
#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;
typedef pair< int,pair<int,int> > PIII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(lo i=1;i<=n;i++)
#define mid (bas+son)/2

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 100005;
const lo mod = 1000000007;

lo n,m,cev,b,a[li],k,vis[1005][1005],isaret[1005][1005],sti,stj,ei,ej,visit[1005][1005],d[1005][1005],cost[1005][1005],cevv;
string s;
char c[1005][1005];
vector<int> v;
queue< PIII > q;

void sp(lo sa,lo su){
	q.push(mp(sa,mp(su,0)));
	while(q.size()){
		lo satir=q.front().fi;
		lo sutun=q.front().se.fi;
		lo para=q.front().se.se;
		q.pop();
		if(visit[satir][sutun]==1) continue;
		visit[satir][sutun]=1;
		d[satir][sutun]=para;
		if(satir>1 && vis[satir-1][sutun]==0) q.push(mp(satir-1,mp(sutun,para+1)));
		if(satir<n && vis[satir+1][sutun]==0) q.push(mp(satir+1,mp(sutun,para+1)));
		if(sutun>1 && vis[satir][sutun-1]==0) q.push(mp(satir,mp(sutun-1,para+1)));
		if(sutun<m && vis[satir][sutun+1]==0) q.push(mp(satir,mp(sutun+1,para+1)));
		//~ if(sa==sti && su==stj) cout<<para<<endl;
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	FOR{
		for(lo j=1;j<=m;j++){
			scanf(" %c",&c[i][j]);
			if(c[i][j]=='T') vis[i][j]=1;
			else if(c[i][j]=='S'){sti=i;stj=j;}
			else if(c[i][j]=='E'){ei=i;ej=j;}
			else if(c[i][j]-'0'>0)	isaret[i][j]=1;
		}
	}
	FOR{
		for(lo j=1;j<=m;j++){
			if(isaret[i][j]==1){memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(i,j);}
			cost[i][j]=d[ei][ej];
		}
	}
	memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(sti,stj);
	cevv=d[ei][ej];
	FOR{
		for(lo j=1;j<=m;j++){
			if(cost[i][j]<=cevv && isaret[i][j]==1) cev+=c[i][j]-'0';
		}
	}
	//~ cout<<sti<<" "<<stj<<endl;
	printf("%lld\n",cev);
	return 0;
}


//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Allah Buyuktur.
//Bismillahirrahmanirrahim
#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;
typedef pair< int,pair<int,int> > PIII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(lo i=1;i<=n;i++)
#define mid (bas+son)/2

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 10000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 100005;
const lo mod = 1000000007;

lo n,m,cev,b,a[li],k,vis[1005][1005],isaret[1005][1005],sti,stj,ei,ej,visit[1005][1005],d[1005][1005],cost[1005][1005],cevv;
string s;
char c[1005][1005];
vector<int> v;
queue< PIII > q;

void sp(lo sa,lo su){
	q.push(mp(sa,mp(su,0)));
	while(q.size()){
		lo satir=q.front().fi;
		lo sutun=q.front().se.fi;
		lo para=q.front().se.se;
		q.pop();
		if(visit[satir][sutun]==1) continue;
		visit[satir][sutun]=1;
		d[satir][sutun]=para;
		if(satir>1 && vis[satir-1][sutun]==0) q.push(mp(satir-1,mp(sutun,para+1)));
		if(satir<n && vis[satir+1][sutun]==0) q.push(mp(satir+1,mp(sutun,para+1)));
		if(sutun>1 && vis[satir][sutun-1]==0) q.push(mp(satir,mp(sutun-1,para+1)));
		if(sutun<m && vis[satir][sutun+1]==0) q.push(mp(satir,mp(sutun+1,para+1)));
		//~ if(sa==sti && su==stj) cout<<para<<endl;
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	FOR{
		for(lo j=1;j<=m;j++){
			scanf(" %c",&c[i][j]);
			if(c[i][j]=='T') vis[i][j]=1;
			else if(c[i][j]=='S'){sti=i;stj=j;}
			else if(c[i][j]=='E'){ei=i;ej=j;}
			else if(c[i][j]-'0'>0)	isaret[i][j]=1;
		}
	}
	sp(ei,ej);
	cevv=d[sti][stj];
	//~ cout<<cevv<<endl;
	FOR{
		for(lo j=1;j<=m;j++){
			//~ cout<<cost[i][j]<<endl;
			if(d[i][j]<=cevv && isaret[i][j]==1){cev+=c[i][j]-'0';}
		}
	}
	//~ cout<<sti<<" "<<stj<<endl;
	printf("%lld\n",cev);
	return 0;
}


//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Allah Buyuktur.
//Bismillahirrahmanirrahim
#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;
typedef pair< int,pair<int,int> > PIII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(lo i=1;i<=n;i++)
#define mid (bas+son)/2

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 10000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 100005;
const lo mod = 1000000007;

lo n,m,cev,b,a[li],k,vis[1005][1005],isaret[1005][1005],sti,stj,ei,ej,visit[1005][1005],d[1005][1005],cost[1005][1005],cevv;
string s;
char c[1005][1005];
vector<int> v;
queue< PIII > q;

void sp(lo sa,lo su){
	q.push(mp(sa,mp(su,0)));
	while(q.size()){
		lo satir=q.front().fi;
		lo sutun=q.front().se.fi;
		lo para=q.front().se.se;
		q.pop();
		if(visit[satir][sutun]==1) continue;
		visit[satir][sutun]=1;
		d[satir][sutun]=para;
		if(satir>1 && vis[satir-1][sutun]==0) q.push(mp(satir-1,mp(sutun,para+1)));
		if(satir<n && vis[satir+1][sutun]==0) q.push(mp(satir+1,mp(sutun,para+1)));
		if(sutun>1 && vis[satir][sutun-1]==0) q.push(mp(satir,mp(sutun-1,para+1)));
		if(sutun<m && vis[satir][sutun+1]==0) q.push(mp(satir,mp(sutun+1,para+1)));
		//~ if(sa==sti && su==stj) cout<<para<<endl;
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	FOR{
		for(lo j=1;j<=m;j++){
			scanf(" %c",&c[i][j]);
			if(c[i][j]=='T') vis[i][j]=1;
			else if(c[i][j]=='S'){sti=i;stj=j;}
			else if(c[i][j]=='E'){ei=i;ej=j;}
			else if(c[i][j]-'0'>0)	isaret[i][j]=1;
		}
	}
	FOR{
		for(lo j=1;j<=m;j++){
			if(isaret[i][j]==1){memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(i,j);}
			if(d[i][j]==0) cost[i][j]=inf;
			else cost[i][j]=d[ei][ej];
		}
	}
	memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(sti,stj);
	cevv=d[ei][ej];
	FOR{
		for(lo j=1;j<=m;j++){
			if(cost[i][j]<=cevv && isaret[i][j]==1) cev+=c[i][j]-'0';
		}
	}
	//~ cout<<sti<<" "<<stj<<endl;
	printf("%lld\n",cev);
	return 0;
}


//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Allah Buyuktur.
//Bismillahirrahmanirrahim
#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;
typedef pair< int,pair<int,int> > PIII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(lo i=1;i<=n;i++)
#define mid (bas+son)/2

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 10000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 100005;
const lo mod = 1000000007;

lo n,m,cev,b,a[li],k,vis[1005][1005],isaret[1005][1005],sti,stj,ei,ej,visit[1005][1005],d[1005][1005],cost[1005][1005],cevv;
string s;
char c[1005][1005];
vector<int> v;
queue< PIII > q;

void sp(lo sa,lo su){
	q.push(mp(sa,mp(su,0)));
	while(q.size()){
		lo satir=q.front().fi;
		lo sutun=q.front().se.fi;
		lo para=q.front().se.se;
		q.pop();
		if(visit[satir][sutun]==1) continue;
		visit[satir][sutun]=1;
		d[satir][sutun]=para;
		if(satir>1 && vis[satir-1][sutun]==0) q.push(mp(satir-1,mp(sutun,para+1)));
		if(satir<n && vis[satir+1][sutun]==0) q.push(mp(satir+1,mp(sutun,para+1)));
		if(sutun>1 && vis[satir][sutun-1]==0) q.push(mp(satir,mp(sutun-1,para+1)));
		if(sutun<m && vis[satir][sutun+1]==0) q.push(mp(satir,mp(sutun+1,para+1)));
		//~ if(sa==sti && su==stj) cout<<para<<endl;
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	FOR{
		for(lo j=1;j<=m;j++){
			scanf(" %c",&c[i][j]);
			if(c[i][j]=='T') vis[i][j]=1;
			else if(c[i][j]=='S'){sti=i;stj=j;}
			else if(c[i][j]=='E'){ei=i;ej=j;}
			else if(c[i][j]-'0'>0)	isaret[i][j]=1;
		}
	}
	sp(sti,stj);
	cevv=d[ei][ej];
	FOR{
		for(lo j=1;j<=m;j++){
			if(isaret[i][j]==1){memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(i,j);}
			//~ cout<<d[ei][j]<<endl;
			if(d[ei][ej]==0) cost[i][j]=inf;
			else cost[i][j]=d[ei][ej];
		}
	}
	//~ memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));

	//~ cout<<cevv<<endl;
	FOR{
		for(lo j=1;j<=m;j++){
			//~ cout<<cost[i][j]<<endl;
			if(cost[i][j]<=cevv && isaret[i][j]==1){cev+=c[i][j]-'0';}
		}
	}
	//~ cout<<sti<<" "<<stj<<endl;
	printf("%lld\n",cev);
	return 0;
}


//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Allah Buyuktur.
//Bismillahirrahmanirrahim
#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;
typedef pair< int,pair<int,int> > PIII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(lo i=1;i<=n;i++)
#define mid (bas+son)/2

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 10000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 100005;
const lo mod = 1000000007;

lo n,m,cev,b,a[li],k,vis[1005][1005],isaret[1005][1005],sti,stj,ei,ej,visit[1005][1005],d[1005][1005],cost[1005][1005],cevv;
string s;
char c[1005][1005];
vector<int> v;
queue< PIII > q;

void sp(lo sa,lo su){
	q.push(mp(sa,mp(su,0)));
	while(q.size()){
		lo satir=q.front().fi;
		lo sutun=q.front().se.fi;
		lo para=q.front().se.se;
		q.pop();
		if(visit[satir][sutun]==1) continue;
		visit[satir][sutun]=1;
		d[satir][sutun]=para;
		if(satir>1 && vis[satir-1][sutun]==0) q.push(mp(satir-1,mp(sutun,para+1)));
		if(satir<n && vis[satir+1][sutun]==0) q.push(mp(satir+1,mp(sutun,para+1)));
		if(sutun>1 && vis[satir][sutun-1]==0) q.push(mp(satir,mp(sutun-1,para+1)));
		if(sutun<m && vis[satir][sutun+1]==0) q.push(mp(satir,mp(sutun+1,para+1)));
		//~ if(sa==sti && su==stj) cout<<para<<endl;
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	FOR{
		for(lo j=1;j<=m;j++){
			scanf(" %c",&c[i][j]);
			if(c[i][j]=='T') vis[i][j]=1;
			else if(c[i][j]=='S'){sti=i;stj=j;}
			else if(c[i][j]=='E'){ei=i;ej=j;}
			else if(c[i][j]-'0'>0)	isaret[i][j]=1;
		}
	}
	sp(ei,ej);
	cevv=d[sti][stj];
	//~ cout<<cevv<<endl;
	FOR{
		for(lo j=1;j<=m;j++){
			//~ cout<<cost[i][j]<<endl;
			if(d[i][j]<=cevv && isaret[i][j]==1 && d[i][j]!=0){cev+=c[i][j]-'0';}
		}
	}
	//~ cout<<sti<<" "<<stj<<endl;
	printf("%lld\n",cev);
	return 0;
}


//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Allah Buyuktur.
//Bismillahirrahmanirrahim
#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;
typedef pair< int,pair<int,int> > PIII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(lo i=1;i<=n;i++)
#define mid (bas+son)/2

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 10000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 100005;
const lo mod = 1000000007;

lo n,m,cev,b,a[li],k,vis[1005][1005],isaret[1005][1005],sti,stj,ei,ej,visit[1005][1005],d[1005][1005],cost[1005][1005],cevv;
string s;
char c[1005][1005];
vector<int> v;
queue< PIII > q;

void sp(lo sa,lo su){
	q.push(mp(sa,mp(su,0)));
	while(q.size()){
		lo satir=q.front().fi;
		lo sutun=q.front().se.fi;
		lo para=q.front().se.se;
		q.pop();
		if(visit[satir][sutun]==1) continue;
		visit[satir][sutun]=1;
		d[satir][sutun]=para;
		if(satir>1 && vis[satir-1][sutun]==0) q.push(mp(satir-1,mp(sutun,para+1)));
		if(satir<n && vis[satir+1][sutun]==0) q.push(mp(satir+1,mp(sutun,para+1)));
		if(sutun>1 && vis[satir][sutun-1]==0) q.push(mp(satir,mp(sutun-1,para+1)));
		if(sutun<m && vis[satir][sutun+1]==0) q.push(mp(satir,mp(sutun+1,para+1)));
		//~ if(sa==sti && su==stj) cout<<para<<endl;
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	FOR{
		for(lo j=1;j<=m;j++){
			scanf(" %c",&c[i][j]);
			if(c[i][j]=='T') vis[i][j]=1;
			else if(c[i][j]=='S'){sti=i;stj=j;}
			else if(c[i][j]=='E'){ei=i;ej=j;}
			else if(c[i][j]-'0'>0)	isaret[i][j]=1;
		}
	}
	FOR{
		for(lo j=1;j<=m;j++){
			if(isaret[i][j]==1){memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(i,j);}
			//~ cout<<d[ei][j]<<endl;
			if(d[ei][ej]==0) cost[i][j]=inf;
			else cost[i][j]=d[ei][ej];
		}
	}
	memset(visit,0,sizeof(visit));memset(d,0,sizeof(d));sp(sti,stj);
	cevv=d[ei][ej];
	//~ cout<<cevv<<endl;
	FOR{
		for(lo j=1;j<=m;j++){
			//~ cout<<cost[i][j]<<endl;
			if(cost[i][j]<=cevv && isaret[i][j]==1){cev+=c[i][j]-'0';}
		}
	}
	//~ cout<<sti<<" "<<stj<<endl;
	printf("%lld\n",cev);
	return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 1005;
const ll mod = 1e9+7;

int n, m, a[N][N], dp[N*N], mn;
vector <int> g[N*N];
pair <int, int> s, f;

void bfs (int ii, int jj) {
    deque <int> x;
    bool used[N*N] = {0};
    x.pb ((ii-1)*mn+jj);
    used[*x.begin()] = 1;
    dp[*x.begin()] = 0;
    while (!x.empty()) {
        for (auto i:g[*x.begin()]) {
            if (!used[i]) {
                x.pb (i);
                dp[i] = dp[*x.begin()] + 1;
                used[i] = 1;
            }
        }
        x.pop_front();
    }
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	mn = max(m, n);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            char ch;
            cin >> ch;
            if (ch == 'E') {
                f = {i, j};
                a[i][j] = 12;
            } else if (ch == 'S') {
                s = {i, j};
                a[i][j] = 11;
            } else if (ch != 'T') {
                a[i][j] = ch-'0'+1;
            }
        }
	}
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (!a[i][j]) continue;
            if (a[i+1][j]) {
                g[(i-1)*mn+j].pb (i*mn+j);
            }
            if (a[i-1][j]) {
                g[(i-1)*mn+j].pb ((i-2)*mn+j);
            }
            if (a[i][j+1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j+1);
            }
            if (a[i][j-1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j-1);
            }
        }
	}
	ll ans = 0;
	bfs (f.first, f.second);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (dp[(i-1)*mn+j] && dp[(i-1)*mn+j] <= dp[(s.first-1)*mn+s.second] && a[i][j] <= 10) {
                ans += a[i][j]-1;
            }
        }
	}
	cout << ans;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 1005;
const ll mod = 1e9+7;

int n, m, a[N][N], dp[N*N], mn;
vector <int> g[N*N];
pair <int, int> s, f;

void bfs (int ii, int jj) {
    deque <int> x;
    bool used[N*N] = {0};
    x.pb ((ii-1)*mn+jj);
    used[*x.begin()] = 1;
    while (!x.empty()) {
        for (auto i:g[*x.begin()]) {
            if (!used[i]) {
                x.pb (i);
                dp[i] = dp[*x.begin()] + 1;
                used[i] = 1;
            }
        }
        x.pop_front();
    }
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	mn = max(m, n);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            char ch;
            cin >> ch;
            if (ch == 'E') {
                f = {i, j};
                a[i][j] = 12;
            } else if (ch == 'S') {
                s = {i, j};
                a[i][j] = 11;
            } else if (ch != 'T') {
                a[i][j] = ch-'0'+1;
            }
        }
	}
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (!a[i][j]) continue;
            if (a[i+1][j]) {
                g[(i-1)*mn+j].pb (i*mn+j);
            }
            if (a[i-1][j]) {
                g[(i-1)*mn+j].pb ((i-2)*mn+j);
            }
            if (a[i][j+1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j+1);
            }
            if (a[i][j-1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j-1);
            }
        }
	}
	ll ans = 0;
	bfs (f.first, f.second);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (dp[(i-1)*mn+j] <= dp[(s.first-1)*mn+s.second] && a[i][j] <= 10 && a[i][j] > 1) {
                ans += a[i][j]-1;
            }
        }
	}
	cout << ans;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 1005;
const ll mod = 1e9+7;

int n, m, a[N][N], dp[N*N], mn;
vector <int> g[N*N];
pair <int, int> s, f;

void bfs (int ii, int jj) {
    deque <int> x;
    bool used[N*N] = {0};
    x.pb ((ii-1)*mn+jj);
    used[*x.begin()] = 1;
    dp[*x.begin()] = 0;
    while (!x.empty()) {
        for (auto i:g[*x.begin()]) {
            if (!used[i]) {
                x.pb (i);
                dp[i] = dp[*x.begin()] + 1;
                used[i] = 1;
            }
        }
        x.pop_front();
    }
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	mn = max(m, n);
	for (int i=1; i<=mn*mn; i++) {
        dp[i] = 1e8;
    }
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            char ch;
            cin >> ch;
            if (ch == 'E') {
                f = {i, j};
                a[i][j] = 12;
            } else if (ch == 'S') {
                s = {i, j};
                a[i][j] = 11;
            } else if (ch != 'T') {
                a[i][j] = ch-'0'+1;
            }
        }
	}
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (!a[i][j]) continue;
            if (a[i+1][j]) {
                g[(i-1)*mn+j].pb (i*mn+j);
            }
            if (a[i-1][j]) {
                g[(i-1)*mn+j].pb ((i-2)*mn+j);
            }
            if (a[i][j+1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j+1);
            }
            if (a[i][j-1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j-1);
            }
        }
	}
	ll ans = 0;
	bfs (f.first, f.second);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (dp[(i-1)*mn+j] <= dp[(s.first-1)*mn+s.second] && a[i][j] <= 10 && a[i][j] > 1) {
                ans += a[i][j]-1;
            }
        }
	}
	cout << ans;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 1005;
const ll mod = 1e9+7;

int n, m, a[N][N], dp[N*N], mn;
vector <int> g[N*N];
pair <int, int> s, f;

void bfs (int ii, int jj) {
    deque <int> x;
    bool used[N*N] = {0};
    x.pb ((ii-1)*mn+jj);
    used[*x.begin()] = 1;
    dp[*x.begin()] = 0;
    while (!x.empty()) {
        for (auto i:g[*x.begin()]) {
            if (!used[i]) {
                x.pb (i);
                dp[i] = dp[*x.begin()] + 1;
                used[i] = 1;
            }
        }
        x.pop_front();
    }
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	mn = max(m, n);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            char ch;
            cin >> ch;
            if (ch == 'E') {
                f = {i, j};
                a[i][j] = 12;
            } else if (ch == 'S') {
                s = {i, j};
                a[i][j] = 11;
            } else if (ch != 'T') {
                a[i][j] = ch-'0'+1;
            }
        }
	}
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (!a[i][j]) continue;
            if (a[i+1][j]) {
                g[(i-1)*mn+j].pb (i*mn+j);
            }
            if (a[i-1][j]) {
                g[(i-1)*mn+j].pb ((i-2)*mn+j);
            }
            if (a[i][j+1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j+1);
            }
            if (a[i][j-1]) {
                g[(i-1)*mn+j].pb ((i-1)*mn+j-1);
            }
        }
	}
	ll ans = 0;
	bfs (f.first, f.second);
	for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (dp[(i-1)*mn+j] && dp[(i-1)*mn+j] <= dp[(s.first-1)*mn+s.second] && a[i][j] <= 10 && a[i][j] > 1) {
                ans += a[i][j]-1;
            }
        }
	}
	cout << ans;
    return 0;
}


/*
    Stay motivated and keep working hard
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll r, c;
char a[1007][1007];
ll t[1007][1007];
bool vis[1007][1007];
ll start_r, start_c, exit_r, exit_c;
ll dr[] = {0, -1, 0, 1};
ll dc[] = {1, 0, -1, 0};
void bfs(ll sr, ll sc){
    vis[sr][sc] = true;
    t[sr][sc] = 0;
    queue<pair<ll, ll>> q;
    q.push({sr, sc});
    while(!q.empty()){
        pair<ll, ll> u = q.front();
        ll ux = u.first, uy = u.second;
        q.pop();
        for(ll i = 0; i < 4; i++){
            if(ux + dr[i] < 0 || ux + dr[i] > r || uy + dc[i] < 0 || uy + dc[i] > c){
                continue;
            }
            if(a[ux+dr[i]][uy+dc[i]] == 'T'){
                continue;
            }
            if(!vis[ux+dr[i]][uy+dc[i]]){
                vis[ux+dr[i]][uy+dc[i]] = true;
                t[ux+dr[i]][uy+dc[i]] = t[ux][uy] + 1;
                q.push({ux+dr[i], uy+dc[i]});
            }
        }
    }
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        // freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> r >> c;
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            cin >> a[i][j];
            if(a[i][j] == 'E'){
                exit_r = i;
                exit_c = j;
            }
            if(a[i][j] == 'S'){
                start_r = i;
                start_c = j;
            }
        }
    }
    bfs(exit_r, exit_c);
    ll mytime = t[start_r][start_c];
    ll b = 0;
    // cout << mytime << "\n";
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            if(a[i][j] == 'E' || a[i][j] == 'S' || a[i][j] == 'T'){
                continue;
            }
            if(t[i][j] <= mytime){
                b += (a[i][j] - '0');
            }
        }
    }
    cout << b << "\n";

    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif
    return 0;
}


/*
    Stay motivated and keep working hard
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF = 0x3f3f3f3f;
ll r, c;
char a[1007][1007];
ll t[1007][1007];
bool vis[1007][1007];
ll start_r, start_c, exit_r, exit_c;
ll dr[] = {0, -1, 0, 1};
ll dc[] = {1, 0, -1, 0};
void bfs(ll sr, ll sc){
    vis[sr][sc] = true;
    t[sr][sc] = 0;
    queue<pair<ll, ll>> q;
    q.push({sr, sc});
    while(!q.empty()){
        pair<ll, ll> u = q.front();
        ll ux = u.first, uy = u.second;
        q.pop();
        for(ll i = 0; i < 4; i++){
            if(ux + dr[i] < 0 || ux + dr[i] > r || uy + dc[i] < 0 || uy + dc[i] > c){
                continue;
            }
            if(a[ux+dr[i]][uy+dc[i]] == 'T'){
                continue;
            }
            if(!vis[ux+dr[i]][uy+dc[i]]){
                vis[ux+dr[i]][uy+dc[i]] = true;
                t[ux+dr[i]][uy+dc[i]] = t[ux][uy] + 1;
                q.push({ux+dr[i], uy+dc[i]});
            }
        }
    }
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        // freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> r >> c;
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            cin >> a[i][j];
            if(a[i][j] == 'E'){
                exit_r = i;
                exit_c = j;
            }
            if(a[i][j] == 'S'){
                start_r = i;
                start_c = j;
            }
        }
    }
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            t[i][j] = INF;
        }
    }
    bfs(exit_r, exit_c);
    ll mytime = t[start_r][start_c];
    ll b = 0;
    // cout << mytime << "\n";
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            if(a[i][j] == 'E' || a[i][j] == 'S' || a[i][j] == 'T'){
                continue;
            }
            if(t[i][j] <= mytime){
                b += (a[i][j] - '0');
            }
        }
    }
    cout << b << "\n";

    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif
    return 0;
}


/*
    Stay motivated and keep working hard
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int r, c;
char a[1007][1007];
int t[1007][1007];
bool vis[1007][1007];
int start_r, start_c, exit_r, exit_c;
int dr[] = {0, -1, 0, 1};
int dc[] = {1, 0, -1, 0};
void bfs(int sr, int sc){
    vis[sr][sc] = true;
    t[sr][sc] = 0;
    queue<pair<int, int>> q;
    q.push({sr, sc});
    while(!q.empty()){
        pair<int, int> u = q.front();
        int ux = u.first, uy = u.second;
        q.pop();
        for(int i = 0; i < 4; i++){
            if(ux + dr[i] < 0 || ux + dr[i] > r || uy + dc[i] < 0 || uy + dc[i] > c){
                continue;
            }
            if(a[ux+dr[i]][uy+dc[i]] == 'T'){
                continue;
            }
            if(!vis[ux+dr[i]][uy+dc[i]]){
                vis[ux+dr[i]][uy+dc[i]] = true;
                t[ux+dr[i]][uy+dc[i]] = t[ux][uy] + 1;
                q.push({ux+dr[i], uy+dc[i]});
            }
        }
    }
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        // freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> r >> c;
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            cin >> a[i][j];
            if(a[i][j] == 'E'){
                exit_r = i;
                exit_c = j;
            }
            if(a[i][j] == 'S'){
                start_r = i;
                start_c = j;
            }
        }
    }
    bfs(exit_r, exit_c);
    int mytime = t[start_r][start_c];
    int b = 0;
    // cout << mytime << "\n";
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            if(a[i][j] == 'E' || a[i][j] == 'S' || a[i][j] == 'T'){
                continue;
            }
            if(t[i][j] <= mytime){
                b += (a[i][j] - '0');
            }
        }
    }
    cout << b << "\n";

    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif
    return 0;
}


/*
    Stay motivated and keep working hard
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF = 0x3f3f3f3f;
ll r, c;
char a[1007][1007];
ll t[1007][1007];
bool vis[1007][1007];
ll start_r, start_c, exit_r, exit_c;
ll dr[] = {0, -1, 0, 1};
ll dc[] = {1, 0, -1, 0};
void bfs(ll sr, ll sc){
    vis[sr][sc] = true;
    t[sr][sc] = 0;
    queue<pair<ll, ll>> q;
    q.push({sr, sc});
    while(!q.empty()){
        pair<ll, ll> u = q.front();
        ll ux = u.first, uy = u.second;
        // cout << ux << " " << uy << "\n";
        q.pop();
        for(ll i = 0; i < 4; i++){
            if(ux + dr[i] < 0 || ux + dr[i] >= r || uy + dc[i] < 0 || uy + dc[i] >= c){
                continue;
            }
            if(a[ux+dr[i]][uy+dc[i]] == 'T'){
                continue;
            }
            if(!vis[ux+dr[i]][uy+dc[i]]){
                vis[ux+dr[i]][uy+dc[i]] = true;
                t[ux+dr[i]][uy+dc[i]] = t[ux][uy] + 1;
                q.push({ux+dr[i], uy+dc[i]});
            }
        }
    }
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        // freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> r >> c;
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            cin >> a[i][j];
            if(a[i][j] == 'E'){
                exit_r = i;
                exit_c = j;
            }
            if(a[i][j] == 'S'){
                start_r = i;
                start_c = j;
            }
        }
    }
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            t[i][j] = INF;
        }
    }
    bfs(exit_r, exit_c);
    ll mytime = t[start_r][start_c];
    ll b = 0;
    // cout << mytime << "\n";
    for(ll i=0;i<r;i++){
        for(ll j=0;j<c;j++){
            if(a[i][j] == 'E' || a[i][j] == 'S' || a[i][j] == 'T'){
                continue;
            }
            if(t[i][j] <= mytime){
                // cout << i << " " << j << " " << t[i][j] << " " << a[i][j] << "\n";
                b += (a[i][j] - '0');
            }
        }
    }
    cout << b << "\n";

    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <iterator>
#define FIO ios_base::sync_with_stdio(false);
#define pb push_back
#define ll long long int
#define Pi 3.1415926536
using namespace std;
int dr[]={1,-1,0,0};
int dc[]={0,0,-1,1};
const int N=1005;
int n , m;
char grid[N][N];
pair<int,int> start;
pair<int,int> end1;
bool vis[N][N];

bool valid(int x , int y){
    return (!vis[x][y] && x>=0 && x<n && y>=0 && y<m && grid[x][y]!='T') ? true : false;
}

int bfs(int x , int y){
    queue<pair<pair<int,int>,int>> nextToVisit;
    nextToVisit.push({{x,y},0});
    while(!nextToVisit.empty()){
        x = nextToVisit.front().first.first;
        y = nextToVisit.front().first.second;
        int c = nextToVisit.front().second;
        nextToVisit.pop();
        if (x==end1.first && y==end1.second) return c;
        if (vis[x][y]) continue;
        vis[x][y]=1;
        for (int k = 0 ;  k < 4 ; ++k){
            int currx = x + dr[k];
            int curry = y + dc[k];
            if (valid(currx,curry)){
                nextToVisit.push({{currx,curry},c+1});
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    for (int i = 0 ; i < n ; ++i){
        for (int j = 0 ; j < m ; ++j){
            cin>>grid[i][j];
            if (grid[i][j]=='S') start={i,j};
            if (grid[i][j]=='E') end1={i,j};
        }
    }
    int ans=0;
    int solved = bfs(start.first,start.second);
    for (int i = 0 ; i < n ; ++i){
        for (int j = 0 ; j < m ; ++j){
            if (grid[i][j]!='0' && grid[i][j]!='S' && grid[i][j]!='E' && grid[i][j]!='T'){
                memset(vis , 0 , sizeof(vis));
                int path = bfs(i,j);
                if (path<=solved) ans+=grid[i][j]-'0';
            }
        }
    }
    cout<<ans;

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <iterator>
#define FIO ios_base::sync_with_stdio(false);
#define pb push_back
#define ll long long int
#define Pi 3.1415926536
using namespace std;
int dr[]={1,-1,0,0};
int dc[]={0,0,-1,1};
const int N=1005;
int n , m;
char grid[N][N];
pair<int,int> start;
pair<int,int> end1;
bool vis[N][N];
ll ansArr[N][N];

bool valid(int x , int y){
    return (!vis[x][y] && x>=0 && x<n && y>=0 && y<m && grid[x][y]!='T') ? true : false;
}

void bfs(int x , int y){
    queue<pair<pair<int,int>,int>> nextToVisit;
    nextToVisit.push({{x,y},0});
    while(!nextToVisit.empty()){
        x = nextToVisit.front().first.first;
        y = nextToVisit.front().first.second;
        int c = nextToVisit.front().second;
        ansArr[x][y]=c;
        nextToVisit.pop();
        if (vis[x][y]) continue;
        vis[x][y]=1;
        for (int k = 0 ;  k < 4 ; ++k){
            int currx = x + dr[k];
            int curry = y + dc[k];
            if (valid(currx,curry)){
                nextToVisit.push({{currx,curry},c+1});
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    for (int i = 0 ; i < n ; ++i){
        for (int j = 0 ; j < m ; ++j){
            cin>>grid[i][j];
            if (grid[i][j]=='S') start={i,j};
            if (grid[i][j]=='E') end1={i,j};
        }
    }
    ll ans=0;
    bfs (end1.first,end1.second);
    ll solved = ansArr[start.first][start.second];
    for (int i = 0 ; i < n ; ++i){
        for (int j = 0 ; j < m ; ++j){
            if (grid[i][j]!='0' && grid[i][j]!='S' && grid[i][j]!='E' && grid[i][j]!='T'){
                if (ansArr[i][j] != 0 && ansArr[i][j]<=solved) ans+=grid[i][j]-'0';
            }
        }
    }
    cout<<ans;

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <iterator>
#define FIO ios_base::sync_with_stdio(false);
#define pb push_back
#define ll long long int
#define Pi 3.1415926536
using namespace std;
int dr[]={1,-1,0,0};
int dc[]={0,0,-1,1};
const int N=1005;
int n , m;
char grid[N][N];
pair<int,int> start;
pair<int,int> end1;
bool vis[N][N];

bool valid(int x , int y){
    return (!vis[x][y] && x>=0 && x<n && y>=0 && y<m && grid[x][y]!='T') ? true : false;
}

int bfs(int x , int y){
    queue<pair<pair<int,int>,int>> nextToVisit;
    nextToVisit.push({{x,y},0});
    while(!nextToVisit.empty()){
        x = nextToVisit.front().first.first;
        y = nextToVisit.front().first.second;
        int c = nextToVisit.front().second;
        nextToVisit.pop();
        if (x==end1.first && y==end1.second) return c;
        if (vis[x][y]) continue;
        vis[x][y]=1;
        for (int k = 0 ;  k < 4 ; ++k){
            int currx = x + dr[k];
            int curry = y + dc[k];
            if (valid(currx,curry)){
                nextToVisit.push({{currx,curry},c+1});
            }
        }
    }
    return 10000000;
}

int main()
{
    cin>>n>>m;
    for (int i = 0 ; i < n ; ++i){
        for (int j = 0 ; j < m ; ++j){
            cin>>grid[i][j];
            if (grid[i][j]=='S') start={i,j};
            if (grid[i][j]=='E') end1={i,j};
        }
    }
    int ans=0;
    int solved = bfs(start.first,start.second);
    for (int i = 0 ; i < n ; ++i){
        for (int j = 0 ; j < m ; ++j){
            if (grid[i][j]!='0' && grid[i][j]!='S' && grid[i][j]!='E' && grid[i][j]!='T'){
                memset(vis , 0 , sizeof(vis));
                int path = bfs(i,j);
                if (path<=solved) ans+=grid[i][j]-'0';
            }
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define pii pair<ll,ll>
#define vii vector<pii>
#define MOD 1000000007LL
#define calc_fact(n) tgamma(n+1)

ll n,m;
vector<vector<char>> mat(1005,vector<char>(1005,'Z'));

bool isSafe(ll x,ll y)
{
    if(x<n and x>=0 and y<m and y>=0)return true;

    return false;
}

ll  find_lev(ll x1,ll y1)
{
    queue<pair<ll,pii>> q;
    q.push({0,{x1,y1}});
    vector<vector<bool>> visited(n,vector<bool>(m,false));visited[x1][y1]=true;
    while(!q.empty())
    {
        ll val=q.front().F,x=q.front().S.F,y=q.front().S.S;
        q.pop();
        if(mat[x][y]=='E')return val;
        if(isSafe(x+1,y) and mat[x+1][y]!='T'){visited[x+1][y]=true;q.push({val+1,{x+1,y}});}
        if(isSafe(x-1,y) and mat[x-1][y]!='T'){visited[x-1][y]=true;q.push({val+1,{x-1,y}});}
        if(isSafe(x,y+1) and mat[x][y+1]!='T'){visited[x][y+1]=true;q.push({val+1,{x,y+1}});}
        if(isSafe(x,y-1) and mat[x][y-1]!='T'){visited[x][y-1]=true;q.push({val+1,{x,y-1}});}
    }
    return -1;
}

signed main()
{
    FIO;ll src1,src2,lev,ans=0;
    cin>>n>>m;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            char ch;cin>>ch;
            if(ch=='S'){src1=i;src2=j;}
            mat[i][j]=ch;
        }
    }
    lev=find_lev(src1,src2);
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(mat[i][j]>'0' and mat[i][j]<='9')
            {
                ll p=find_lev(i,j);
                if(p!=-1 and p<=lev)ans+=(mat[i][j]-'0');
            }
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define pii pair<ll,ll>
#define vii vector<pii>
#define MOD 1000000007LL
#define calc_fact(n) tgamma(n+1)
#define INF LONG_LONG_MAX

bool isSafe(ll x,ll y,ll n,ll m)
{
    if(x<n and x>=0 and y<m and y>=0)return true;

    return false;
}

signed main()
{
    FIO;
    ll n,m,src1,src2,dest1,dest2,ans=0;
    cin>>n>>m;
    vector<vector<char>> mat(n,vector<char>(m,'Z'));
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>mat[i][j];
            if(mat[i][j]=='S'){src1=i;src2=j;}
            if(mat[i][j]=='E'){dest1=i;dest2=j;}
        }
    }
    vector<vi> dist(n,vi(m,INF));dist[dest1][dest2]=0;
    queue<pair<ll,pii>> q;
    q.push({0,{dest1,dest2}});
    while(!q.empty())
    {
        ll val=q.front().F,x=q.front().S.F,y=q.front().S.S;
        q.pop();

        if(isSafe(x+1,y,n,m) and dist[x+1][y]==INF and mat[x+1][y]!='T'){dist[x+1][y]=val+1;q.push({val+1,{x+1,y}});}
        if(isSafe(x-1,y,n,m) and dist[x-1][y]==INF and mat[x-1][y]!='T'){dist[x-1][y]=val+1;q.push({val+1,{x-1,y}});}
        if(isSafe(x,y+1,n,m) and dist[x][y+1]==INF and mat[x][y+1]!='T'){dist[x][y+1]=val+1;q.push({val+1,{x,y+1}});}
        if(isSafe(x,y-1,n,m) and dist[x][y-1]==INF and mat[x][y-1]!='T'){dist[x][y-1]=val+1;q.push({val+1,{x,y-1}});}
    }
    ll lev=dist[src1][src2];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(mat[i][j]>'0' and mat[i][j]<='9' and dist[i][j]<=lev)ans+=(mat[i][j]-'0');
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define pii pair<ll,ll>
#define vii vector<pii>
#define MOD 1000000007LL
#define calc_fact(n) tgamma(n+1)

ll n,m;
vector<vector<char>> mat(1005,vector<char>(1005,'Z'));

bool isSafe(ll x,ll y)
{
    if(x<n and x>=0 and y<m and y>=0)return true;

    return false;
}

ll  find_lev(ll x1,ll y1)
{
    queue<pair<ll,pii>> q;
    q.push({0,{x1,y1}});
    vector<vector<bool>> visited(n,vector<bool>(m,false));visited[x1][y1]=true;
    while(!q.empty())
    {
        ll val=q.front().F,x=q.front().S.F,y=q.front().S.S;
        q.pop();
        if(mat[x][y]=='E')return val;
        if(isSafe(x+1,y) and mat[x+1][y]!='T'){visited[x+1][y]=true;q.push({val+1,{x+1,y}});}
        if(isSafe(x-1,y) and mat[x-1][y]!='T'){visited[x-1][y]=true;q.push({val+1,{x-1,y}});}
        if(isSafe(x,y+1) and mat[x][y+1]!='T'){visited[x][y+1]=true;q.push({val+1,{x,y+1}});}
        if(isSafe(x,y-1) and mat[x][y-1]!='T'){visited[x][y-1]=true;q.push({val+1,{x,y-1}});}
    }
    return -1;
}

signed main()
{
    FIO;ll src1,src2,lev,ans=0;
    cin>>n>>m;map<pii,ll> mp;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            char ch;cin>>ch;
            if(ch=='S'){src1=i;src2=j;}
            if(ch!='0' and ch!='E' and ch!='T' and ch!='S')mp[{i,j}]=ch-'0';
            mat[i][j]=ch;
        }
    }
    lev=find_lev(src1,src2);
    for(auto it:mp)
    {
        ll l=find_lev(it.F.F,it.F.S);
        if(l!=-1 and l<=lev)ans+=it.S;
    }
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define LL long long
#define ULL unsigned long long
#define LB lower_bound
#define MOD1 1000000007
#define MOD2 1000000009
#define pii pair<int, int>
#define loop(i, a, b) for (int i = a; i < b; i++)
LL n, m;
int movex[] = {0, 0, 1, -1};
int movey[] = {1, -1, 0, 0};

bool valid(int x, int y, int r, int c, vector<vector<char>> &arr)
{
    return x >= 0 && y >= 0 && x < r && y < c && arr[x][y] != 'T';
}

int main()
{
    static const int _ = []() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        return 0;
    }();

    int r, c;
    cin >> r >> c;
    pii ex;
    pii st;
    vector<vector<char>> arr(r, vector<char>(c));
    vector<vector<int>> dist(r, vector<int>(c, -1));
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin >> arr[i][j];
            if (arr[i][j] == 'E')
            {
                ex = make_pair(i, j);
            }
            if (arr[i][j] == 'S')
            {
                st = make_pair(i, j);
            }
        }
    }
    queue<pii> q;
    q.emplace(ex);
    dist[ex.first][ex.second] = 0;
    int mydist;
    while (q.size())
    {
        auto front = q.front();
        q.pop();
        int x = front.first;
        int y = front.second;

        for (int k = 0; k < 4; k++)
        {
            int newx = x + movex[k];
            int newy = y + movey[k];
            if (!valid(newx, newy, r, c, arr) || dist[newx][newy] != -1)
                continue;
            dist[newx][newy] = dist[x][y] + 1;

            if (newx == st.first && newy == st.second)
                mydist = dist[newx][newy];
            q.emplace(newx, newy);
        }
    }

    int ans = 0;
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            if (arr[i][j] >= '0' && arr[i][j] <= '9')
            {
                if (dist[i][j] <= mydist)
                {
                    ans += (arr[i][j] - '0');
                }
            }
        }
    }

    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define LL long long
#define ULL unsigned long long
#define LB lower_bound
#define MOD1 1000000007
#define MOD2 1000000009
#define pii pair<int, int>
#define loop(i, a, b) for (int i = a; i < b; i++)
LL n, m;
int movex[] = {0, 0, 1, -1};
int movey[] = {1, -1, 0, 0};

bool valid(int x, int y, int r, int c, vector<vector<char>> &arr)
{
    return x >= 0 && y >= 0 && x < r && y < c && arr[x][y] != 'T';
}

int main()
{
    static const int _ = []() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        return 0;
    }();

    int r, c;
    cin >> r >> c;
    pii ex;
    pii st;
    vector<vector<char>> arr(r, vector<char>(c));
    vector<vector<int>> dist(r, vector<int>(c, -1));
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin >> arr[i][j];
            if (arr[i][j] == 'E')
            {
                ex = make_pair(i, j);
            }
            if (arr[i][j] == 'S')
            {
                st = make_pair(i, j);
            }
        }
    }
    queue<pii> q;
    q.emplace(ex);
    dist[ex.first][ex.second] = 0;
    int mydist;
    while (q.size())
    {
        auto front = q.front();
        q.pop();
        int x = front.first;
        int y = front.second;

        for (int k = 0; k < 4; k++)
        {
            int newx = x + movex[k];
            int newy = y + movey[k];
            if (!valid(newx, newy, r, c, arr) || dist[newx][newy] != -1)
                continue;
            dist[newx][newy] = dist[x][y] + 1;

            if (newx == st.first && newy == st.second)
                mydist = dist[newx][newy];
            q.emplace(newx, newy);
        }
    }

    int ans = 0;
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            if (arr[i][j] >= '0' && arr[i][j] <= '9')
            {
                if (dist[i][j] != -1 && dist[i][j] <= mydist)
                {
                    ans += arr[i][j];
                }
            }
        }
    }

    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define LL long long
#define ULL unsigned long long
#define LB lower_bound
#define MOD1 1000000007
#define MOD2 1000000009
#define pii pair<int, int>
#define loop(i, a, b) for (int i = a; i < b; i++)
LL n, m;
int movex[] = {0, 0, 1, -1};
int movey[] = {1, -1, 0, 0};

bool valid(int x, int y, int r, int c, vector<vector<char>> &arr)
{
    return x >= 0 && y >= 0 && x < r && y < c && arr[x][y] != 'T';
}

int main()
{
    static const int _ = []() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        return 0;
    }();

    int r, c;
    cin >> r >> c;
    pii ex;
    pii st;
    vector<vector<char>> arr(r, vector<char>(c));
    vector<vector<int>> dist(r, vector<int>(c, -1));
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin >> arr[i][j];
            if (arr[i][j] == 'E')
            {
                ex = make_pair(i, j);
            }
            if (arr[i][j] == 'S')
            {
                st = make_pair(i, j);
            }
        }
    }
    queue<pii> q;
    q.emplace(ex);
    dist[ex.first][ex.second] = 0;
    int mydist;
    while (q.size())
    {
        auto front = q.front();
        q.pop();
        int x = front.first;
        int y = front.second;

        for (int k = 0; k < 4; k++)
        {
            int newx = x + movex[k];
            int newy = y + movey[k];
            if (!valid(newx, newy, r, c, arr) || dist[newx][newy] != -1)
                continue;
            dist[newx][newy] = dist[x][y] + 1;

            if (newx == st.first && newy == st.second)
                mydist = dist[newx][newy];
            q.emplace(newx, newy);
        }
    }

    int ans = 0;
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            if (arr[i][j] >= '0' && arr[i][j] <= '9')
            {
                if (dist[i][j] != -1 && dist[i][j] <= mydist)
                {
                    ans += (arr[i][j] - '0');
                }
            }
        }
    }

    cout << ans << "\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1e15
#define f first
#define s second
using namespace std;
vector<int>adj[1001001];
ll visited[1001001],dist[1001001],r,c,x,start;
string s[1009];
void dijkstra()
{
ll i,j;
memset(visited,0,sizeof(visited));
	for(i=0;i<=r*c;i++)
	dist[i]=inf;
dist[x]=0;
multiset<pair<ll,ll> > Q;
Q.insert({0,x});
	while(!Q.empty())
	{
	pair<ll,ll> p=*Q.begin();
	Q.erase(Q.begin());
	ll v=p.s;
		if(visited[v])
		continue;
	visited[v]=1;
		for(j=0;j<adj[v].size();j++)
		{
		ll u=adj[v][j];
			if(dist[u]>dist[v]+1)
			{
			dist[u]=dist[v]+1;
			Q.insert({dist[u],u});
			}
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,j;
cin>>r>>c;
	for(i=0;i<r;i++)cin>>s[i];
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(s[i][j]=='T')
			continue;
			if((i>0)&&(s[i-1][j]!='T'))
			adj[i*c+j].push_back((i-1)*c+j);
			if((i<r-1)&&(s[i+1][j]!='T'))
			adj[i*c+j].push_back((i+1)*c+j);
			if((j>0)&&(s[i][j-1]!='T'))
			adj[i*c+j].push_back((i)*c+j-1);
			if((j<c-1)&&(s[i][j+1]!='T'))
			adj[i*c+j].push_back((i)*c+j+1);
		 	if(s[i][j]=='E')
			x=i*c+j;
			if(s[i][j]=='S')
			start=i*c+j;
		}
	}
memset(visited,0,sizeof(visited));
dijkstra();
ll sum=0;
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if((s[i][j]>='0')&&(s[i][j]<='9')&&(dist[i*c+j]<=dist[start]))
			sum+=s[i][j]-'0';
		}
	}
cout<<sum<<endl;
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1e15
using namespace std;
vector<int>adj[1001001];
ll visited[1001001],dist[1001001],r,c,x,start;
string s[1009];
void dijkstra()
{
ll i,j;
memset(visited,0,sizeof(visited));
	for(i=0;i<=r*c;i++)
	dist[i]=inf;
dist[x]=0;
multiset<ll> Q;
Q.insert(x);
	while(!Q.empty())
	{
	ll p=*Q.begin();
	Q.erase(Q.begin());
		if(visited[p])
		continue;
	visited[p]=1;
		for(j=0;j<adj[p].size();j++)
		{
		ll u=adj[p][j];
			if(dist[u]>dist[p]+1)
			{
			dist[u]=dist[p]+1;
			Q.insert(u);
			}
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,j;
cin>>r>>c;
	for(i=0;i<r;i++)cin>>s[i];
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(s[i][j]=='T')
			continue;
			if((i>0)&&(s[i-1][j]!='T'))
			adj[i*c+j].push_back((i-1)*c+j);
			if((i<r-1)&&(s[i+1][j]!='T'))
			adj[i*c+j].push_back((i+1)*c+j);
			if((j>0)&&(s[i][j-1]!='T'))
			adj[i*c+j].push_back((i)*c+j-1);
			if((j<c-1)&&(s[i][j+1]!='T'))
			adj[i*c+j].push_back((i)*c+j+1);
		 	if(s[i][j]=='E')
			x=i*c+j;
			if(s[i][j]=='S')
			start=i*c+j;
		}
	}
memset(visited,0,sizeof(visited));
dijkstra();
ll sum=0;
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if((s[i][j]>='0')&&(s[i][j]<='9')&&(dist[i*c+j]<=dist[start]))
			sum+=s[i][j]-'0';
		}
	}
cout<<sum<<endl;
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1e15
using namespace std;
vector<int>adj[1001001];
ll visited[1001001],dist[1001001],r,c,x,start;
string s[1009];
void dijkstra()
{
ll i,j;
memset(visited,0,sizeof(visited));
	for(i=0;i<=r*c;i++)
	dist[i]=inf;
dist[x]=0;
set<ll> Q;
Q.insert(x);
	while(!Q.empty())
	{
	ll p=*Q.begin();
	Q.erase(Q.begin());
		if(visited[p])
		continue;
	visited[p]=1;
		for(j=0;j<adj[p].size();j++)
		{
		ll u=adj[p][j];
			if(dist[u]>dist[p]+1)
			{
			dist[u]=dist[p]+1;
			Q.insert(u);
			}
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,j;
cin>>r>>c;
	for(i=0;i<r;i++)cin>>s[i];
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(s[i][j]=='T')
			continue;
			if((i>0)&&(s[i-1][j]!='T'))
			adj[i*c+j].push_back((i-1)*c+j);
			if((i<r-1)&&(s[i+1][j]!='T'))
			adj[i*c+j].push_back((i+1)*c+j);
			if((j>0)&&(s[i][j-1]!='T'))
			adj[i*c+j].push_back((i)*c+j-1);
			if((j<c-1)&&(s[i][j+1]!='T'))
			adj[i*c+j].push_back((i)*c+j+1);
		 	if(s[i][j]=='E')
			x=i*c+j;
			if(s[i][j]=='S')
			start=i*c+j;
		}
	}
memset(visited,0,sizeof(visited));
dijkstra();
ll sum=0;
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if((s[i][j]>='0')&&(s[i][j]<='9')&&(dist[i*c+j]<=dist[start]))
			sum+=s[i][j]-'0';
		}
	}
cout<<sum<<endl;
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define inf 1e15
using namespace std;
vector<int>adj[1000001];
ll visited[1000001],dist[1000001],r,c,x,start;
string s[1001];
void dijkstra()
{
ll i,j;
memset(visited,0,sizeof(visited));
	for(i=0;i<=r*c;i++)
	dist[i]=inf;
dist[x]=0;
set<ll> Q;
Q.insert(x);
	while(!Q.empty())
	{
	ll p=*Q.begin();
	Q.erase(Q.begin());
		if(visited[p])
		continue;
	visited[p]=1;
		for(j=0;j<adj[p].size();j++)
		{
		ll u=adj[p][j];
			if(dist[u]>dist[p]+1)
			{
			dist[u]=dist[p]+1;
			Q.insert(u);
			}
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,j;
cin>>r>>c;
	for(i=0;i<r;i++)cin>>s[i];
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(s[i][j]=='T')
			continue;
			if((i>0)&&(s[i-1][j]!='T'))
			adj[i*c+j].push_back((i-1)*c+j);
			if((i<r-1)&&(s[i+1][j]!='T'))
			adj[i*c+j].push_back((i+1)*c+j);
			if((j>0)&&(s[i][j-1]!='T'))
			adj[i*c+j].push_back((i)*c+j-1);
			if((j<c-1)&&(s[i][j+1]!='T'))
			adj[i*c+j].push_back((i)*c+j+1);
		 	if(s[i][j]=='E')
			x=i*c+j;
			if(s[i][j]=='S')
			start=i*c+j;
		}
	}
memset(visited,0,sizeof(visited));
dijkstra();
ll sum=0;
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if((s[i][j]>='0')&&(s[i][j]<='9')&&(dist[i*c+j]<=dist[start]))
			sum+=s[i][j]-'0';
		}
	}
cout<<sum<<endl;
return 0;
}


#include<bits/stdc++.h>
//#include<triple.h>
#include<cmath>
using namespace std;

#define ll long long
typedef vector<ll> vi;
typedef vector<vi > vvi;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define repa(i,n) for(ll i=1;i<n;i++)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define pb push_back
typedef pair<ll,pair<ll,ll> > pii;
#define inf 1e10

bool prime[100005]={false};
void sieve(){
    prime[0]=true;
    prime[1]=true;
    for(ll i=2;i<100005;i++){
        if(prime[i]==false){
            for(ll j=i*i;j<100005;j+=i)
                prime[j]=true;
                }
            }

}

ll r,c,ex,ey,hor[]={1,-1,0,0},ver[]={0,0,1,-1},ans=0;
pair<ll,ll> exi,start;
char a[3004][3004];
bool vis[3004][3004]={false};
ll moves[3004][3004]={0};

 /*void dfs(ll x,ll y){
    if(x>=r || x<0 || y<0 || y>=c)
        return;
    if(a[x][y]=='T')
        return;
    rep(i,4){
        ex=x+hor[i];
        ey=y+ver[i];
        if(vis[ex][ey])
            continue;
        vis[ex][ey]=true;
        moves[ex][ey]=moves[x][y]+1;
        dfs(ex,ey);
    }
 }
*/

pair<ll,ll> v;

void bfs(ll x,ll y){
    queue<pair<ll,ll> > q;
    q.push(mp(x,y));
    vis[x][y]=true;
    while(!q.empty()){
        v=q.front();
        q.pop();
        if(v.F<0 || v.F>=r || v.S<0 || v.S>=c)
            continue;
        if(a[v.F][v.S]=='T')
            continue;
        rep(i,4){
            ex=v.F+hor[i];
            ey=v.S+ver[i];
            if(ex>=r || ex<0 || ey<0 || ey>=c)
            continue;
            if(vis[ex][ey])
                continue;
            vis[ex][ey]=true;
            moves[ex][ey]=moves[v.F][v.S]+1;
            q.push(mp(ex,ey));
        }
    }
}

void solve(){
    cin>>r>>c;
    rep(i,r){
        rep(j,c){
        cin>>a[i][j];
        if(a[i][j]=='E')
             exi=mp(i,j);
        if(a[i][j]=='S')
            start=mp(i,j);
        }
    }
    rep(i,3003)
    rep(j,3003)
    moves[i][j]=1e10;
    vis[exi.F][exi.S]=true;
    moves[exi.F][exi.S]=0;
    bfs(exi.F,exi.S);
    rep(i,r){
        rep(j,c){
            if(isdigit(a[i][j]) && a[i][j]-'0' && moves[i][j]<=moves[start.F][start.S]){
                //cout<<"inside  ";
                ans+=a[i][j]-'0';
            }
            //cout<<i<<" "<<j<<" "<<a[i][j]<<" "<<moves[i][j]<<"\n";
        }
    }
    cout<<ans;
    //cout<<"      "<<moves[start.F][start.S];
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll T=1;
    //cin>>T;
    while(T--){
    solve();}
    return 0;
}


#include<bits/stdc++.h>
//#include<triple.h>
#include<cmath>
using namespace std;

#define ll long long
typedef vector<ll> vi;
typedef vector<vi > vvi;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define repa(i,n) for(ll i=1;i<n;i++)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define pb push_back
typedef pair<ll,pair<ll,ll> > pii;
#define inf 1e10

bool prime[100005]={false};
void sieve(){
    prime[0]=true;
    prime[1]=true;
    for(ll i=2;i<100005;i++){
        if(prime[i]==false){
            for(ll j=i*i;j<100005;j+=i)
                prime[j]=true;
                }
            }

}

ll r,c,ex,ey,hor[]={1,-1,0,0},ver[]={0,0,1,-1},ans=0;
pair<ll,ll> exi,start;
char a[3004][3004];
bool vis[3004][3004]={false};
ll moves[3004][3004]={0};

 /*void dfs(ll x,ll y){
    if(x>=r || x<0 || y<0 || y>=c)
        return;
    if(a[x][y]=='T')
        return;
    rep(i,4){
        ex=x+hor[i];
        ey=y+ver[i];
        if(vis[ex][ey])
            continue;
        vis[ex][ey]=true;
        moves[ex][ey]=moves[x][y]+1;
        dfs(ex,ey);
    }
 }
*/

pair<ll,ll> v;

void bfs(ll x,ll y){
    queue<pair<ll,ll> > q;
    q.push(mp(x,y));
    vis[x][y]=true;
    while(!q.empty()){
        v=q.front();
        q.pop();
        if(v.F<0 || v.F>=r || v.S<0 || v.S>=c)
            continue;
        if(a[v.F][v.S]=='T')
            continue;
        rep(i,4){
            ex=v.F+hor[i];
            ey=v.S+ver[i];
            if(ex>=r || ex<0 || ey<0 || ey>=c)
        return;
            if(vis[ex][ey])
                continue;
            vis[ex][ey]=true;
            moves[ex][ey]=moves[v.F][v.S]+1;
            q.push(mp(ex,ey));
        }
    }
}

void solve(){
    cin>>r>>c;
    rep(i,r){
        rep(j,c){
        cin>>a[i][j];
        if(a[i][j]=='E')
             exi=mp(i,j);
        if(a[i][j]=='S')
            start=mp(i,j);
        }
    }
    vis[exi.F][exi.S]=true;
    bfs(exi.F,exi.S);
    rep(i,r){
        rep(j,c){
            if(isdigit(a[i][j]) && a[i][j]-'0' && moves[i][j]<=moves[start.F][start.S]){
                //cout<<"inside  ";
                ans+=a[i][j]-'0';
            }
            //cout<<i<<" "<<j<<" "<<a[i][j]<<" "<<moves[i][j]<<"\n";
        }
    }
    cout<<ans;
    //cout<<"      "<<moves[start.F][start.S];
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll T=1;
    //cin>>T;
    while(T--){
    solve();}
    return 0;
}


#include<bits/stdc++.h>
//#include<triple.h>
#include<cmath>
using namespace std;

#define ll long long
typedef vector<ll> vi;
typedef vector<vi > vvi;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define repa(i,n) for(ll i=1;i<n;i++)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define pb push_back
typedef pair<ll,pair<ll,ll> > pii;
#define inf 1e10

bool prime[100005]={false};
void sieve(){
    prime[0]=true;
    prime[1]=true;
    for(ll i=2;i<100005;i++){
        if(prime[i]==false){
            for(ll j=i*i;j<100005;j+=i)
                prime[j]=true;
                }
            }

}

ll r,c,ex,ey,hor[]={1,-1,0,0},ver[]={0,0,1,-1},ans=0;
pair<ll,ll> exi,start;
char a[3004][3004];
bool vis[3004][3004]={false};
ll moves[3004][3004]={0};

 /*void dfs(ll x,ll y){
    if(x>=r || x<0 || y<0 || y>=c)
        return;
    if(a[x][y]=='T')
        return;
    rep(i,4){
        ex=x+hor[i];
        ey=y+ver[i];
        if(vis[ex][ey])
            continue;
        vis[ex][ey]=true;
        moves[ex][ey]=moves[x][y]+1;
        dfs(ex,ey);
    }
 }
*/

pair<ll,ll> v;

void bfs(ll x,ll y){
    queue<pair<ll,ll> > q;
    q.push(mp(x,y));
    vis[x][y]=true;
    while(!q.empty()){
        v=q.front();
        q.pop();
        if(v.F<0 || v.F>=r || v.S<0 || v.S>=c)
            continue;
        if(a[v.F][v.S]=='T')
            continue;
        rep(i,4){
            ex=v.F+hor[i];
            ey=v.S+ver[i];
            if(ex>=r || ex<0 || ey<0 || ey>=c)
            continue;
            if(vis[ex][ey])
                continue;
            vis[ex][ey]=true;
            moves[ex][ey]=moves[v.F][v.S]+1;
            q.push(mp(ex,ey));
        }
    }
}

void solve(){
    cin>>r>>c;
    rep(i,r){
        rep(j,c){
        cin>>a[i][j];
        if(a[i][j]=='E')
             exi=mp(i,j);
        if(a[i][j]=='S')
            start=mp(i,j);
        }
    }
    vis[exi.F][exi.S]=true;
    bfs(exi.F,exi.S);
    rep(i,r){
        rep(j,c){
            if(isdigit(a[i][j]) && a[i][j]-'0' && moves[i][j]<=moves[start.F][start.S]){
                //cout<<"inside  ";
                ans+=a[i][j]-'0';
            }
            //cout<<i<<" "<<j<<" "<<a[i][j]<<" "<<moves[i][j]<<"\n";
        }
    }
    cout<<ans;
    //cout<<"      "<<moves[start.F][start.S];
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll T=1;
    //cin>>T;
    while(T--){
    solve();}
    return 0;
}


#include<bits/stdc++.h>
//#include<triple.h>
#include<cmath>
using namespace std;

#define ll long long
typedef vector<ll> vi;
typedef vector<vi > vvi;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define repa(i,n) for(ll i=1;i<n;i++)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define pb push_back
typedef pair<ll,pair<ll,ll> > pii;
#define inf 1e10

bool prime[100005]={false};
void sieve(){
    prime[0]=true;
    prime[1]=true;
    for(ll i=2;i<100005;i++){
        if(prime[i]==false){
            for(ll j=i*i;j<100005;j+=i)
                prime[j]=true;
                }
            }

}

ll r,c,ex,ey,hor[]={1,-1,0,0},ver[]={0,0,1,-1},ans=0;
pair<ll,ll> exi,start;
char a[3004][3004];
bool vis[3004][3004]={false};
ll moves[3004][3004]={0};

 /*void dfs(ll x,ll y){
    if(x>=r || x<0 || y<0 || y>=c)
        return;
    if(a[x][y]=='T')
        return;
    rep(i,4){
        ex=x+hor[i];
        ey=y+ver[i];
        if(vis[ex][ey])
            continue;
        vis[ex][ey]=true;
        moves[ex][ey]=moves[x][y]+1;
        dfs(ex,ey);
    }
 }
*/

pair<ll,ll> v;

void bfs(ll x,ll y){
    queue<pair<ll,ll> > q;
    q.push(mp(x,y));
    vis[x][y]=true;
    while(!q.empty()){
        v=q.front();
        q.pop();
        if(v.F<0 || v.F>=r || v.S<0 || v.S>=c)
            continue;
        if(a[v.F][v.S]=='T')
            continue;
        rep(i,4){
            ex=v.F+hor[i];
            ey=v.S+ver[i];
            if(vis[ex][ey])
                continue;
            vis[ex][ey]=true;
            moves[ex][ey]=moves[v.F][v.S]+1;
            q.push(mp(ex,ey));
        }
    }
}

void solve(){
    cin>>r>>c;
    rep(i,r){
        rep(j,c){
        cin>>a[i][j];
        if(a[i][j]=='E')
             exi=mp(i,j);
        if(a[i][j]=='S')
            start=mp(i,j);
        }
    }
    vis[exi.F][exi.S]=true;
    bfs(exi.F,exi.S);
    rep(i,r){
        rep(j,c){
            if(isdigit(a[i][j]) && a[i][j]-'0' && moves[i][j]<=moves[start.F][start.S]){
                //cout<<"inside  ";
                ans+=a[i][j]-'0';
            }
            //cout<<i<<" "<<j<<" "<<a[i][j]<<" "<<moves[i][j]<<"\n";
        }
    }
    cout<<ans;
    //cout<<"      "<<moves[start.F][start.S];
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll T=1;
    //cin>>T;
    while(T--){
    solve();}
    return 0;
}


///****In the name of Allah, most Gracious, most Compassionate****//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

#define NL cout<<"\n";
#define ALL(a) a.begin(), a.end()
#define DBG(a) cout<< #a <<" --> "<<(a)<<endl;
#define FastIO ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
    os<<"{"<<p.first<<","<<p.second<<"}";
    return os;
}

const int N=1e3+5;
const int oo=1e9+7;

bool vis[N][N] ;
int dist[N][N];
int r,c;

pii st, en ;

string s[N];
int dx  [] = { 1,0,-1,0};
int dy  [] = { 0,1,0,-1};
void bfs( pii u)
{
    vis[u.first][u.second] = 1;
    queue<pii> q;
    q.emplace(u);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        assert(s[u.first][u.second]!='T');
        for(int i=0; i<4; i++)
        {
            pii v = { u.first + dx[i], u.second + dy[i]};
            if(v.first<0 or v.first >= r)
                continue;
            if(v.second<0 or v.second >= c)
                continue;
            if(s[v.first][v.second]=='T') continue;
            if(vis[v.first][v.second]) continue;
            vis[v.first][v.second] = 1;
            dist[v.first][v.second] = dist[u.first ] [ u.second] + 1;
            q.emplace(v);
        }
    }
}
int32_t main()
{
    FastIO;
    cin>>r>>c;
    st = {-1,-1};
    en = {-1,-1};
    for(int i=0; i<r; i++)
    {
        cin>>s[i];
        for(int j=0; j<c; j++)
            if(s[i][j] == 'S')
                st = {i, j };
            else if(s[i][j] == 'E')
                en = {i, j};
    }
    assert(st!=make_pair(-1,-1));
    assert(en!=make_pair(-1,-1));
    int ans=0;
    bfs(en);
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
            if(s[i][j]!='T' and s[i][j]!='S' and s[i][j]!='E') ans+= (s[i][j]-'0')*(dist[i][j] <= dist[st.first][st.second]);
    }
    cout<<ans<<"\n";
}


///****In the name of Allah, most Gracious, most Compassionate****//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

#define NL cout<<"\n";
#define ALL(a) a.begin(), a.end()
#define DBG(a) cout<< #a <<" --> "<<(a)<<endl;
#define FastIO ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
    os<<"{"<<p.first<<","<<p.second<<"}";
    return os;
}

const int N=1e3+5;
const int oo=1e9+7;

bool vis[N][N] ;
int dist[N][N];
int r,c;

pii st, en ;

string s[N];
int dx  [] = { 1,0,-1,0};
int dy  [] = { 0,1,0,-1};
void bfs( pii u)
{
    vis[u.first][u.second] = 1;
    queue<pii> q;
    q.emplace(u);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            pii v = { u.first + dx[i], u.second + dy[i]};
            if(v.first<0 or v.first >= r)
                continue;
            if(v.second<0 or v.second >= c)
                continue;
            if(s[v.first][v.second]=='T') continue;
            if(vis[v.first][v.second]) continue;
            vis[v.first][v.second] = 1;
            dist[v.first][v.second] = dist[u.first ] [ u.second] + 1;
            q.emplace(v);
        }
    }
}
int32_t main()
{
    FastIO;
    cin>>r>>c;
    for(int i=0; i<r; i++)
    {
        cin>>s[i];
        for(int j=0; j<c; j++)
            if(s[i][j] == 'S')
                st = {i, j };
            else if(s[i][j] == 'E')
                en = {i, j};
    }
    int ans=0;
    bfs(en);
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
            if(s[i][j]!='T' and s[i][j]!='S' and s[i][j]!='E') ans+= (s[i][j]-'0')*(dist[i][j] <= dist[st.first][st.second]);
    }
    cout<<ans<<"\n";
}


///****In the name of Allah, most Gracious, most Compassionate****//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

#define NL cout<<"\n";
#define ALL(a) a.begin(), a.end()
#define DBG(a) cout<< #a <<" --> "<<(a)<<endl;
#define FastIO ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
    os<<"{"<<p.first<<","<<p.second<<"}";
    return os;
}

const int N=1e3+5;
const int oo=1e9+7;

bool vis[N][N] ;
int dist[N][N];
int r,c;

pii st, en ;

string s[N];
int dx  [] = { 1,0,-1,0};
int dy  [] = { 0,1,0,-1};
void bfs( pii u)
{
    dist[u.first][u.second] = 0;
    vis[u.first][u.second] = 1;
    queue<pii> q;
    q.emplace(u);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        assert(s[u.first][u.second]!='T');
        for(int i=0; i<4; i++)
        {
            pii v = { u.first + dx[i], u.second + dy[i]};
            if(v.first<0 or v.first >= r)
                continue;
            if(v.second<0 or v.second >= c)
                continue;
            if(s[v.first][v.second]=='T') continue;
            if(vis[v.first][v.second]) continue;
            vis[v.first][v.second] = 1;
            dist[v.first][v.second] = dist[u.first ] [ u.second] + 1;
            q.emplace(v);
        }
    }
}
int32_t main()
{
    FastIO;
    cin>>r>>c;
    st = {-1,-1};
    en = {-1,-1};
    for(int i=0; i<r; i++)
    {
        cin>>s[i];
        for(int j=0; j<c; j++)
            if(s[i][j] == 'S')
                st = {i, j };
            else if(s[i][j] == 'E')
                en = {i, j};
            else dist[i][j] = oo;
    }
    assert(st!=make_pair(-1,-1));
    assert(en!=make_pair(-1,-1));
    int ans=0;
    bfs(en);
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
            if(s[i][j]!='T' and s[i][j]!='S' and s[i][j]!='E') ans+= (s[i][j]-'0')*(dist[i][j] <= dist[st.first][st.second]);
    }
    cout<<ans<<"\n";
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1010;
const int inf = 1000000000;

int main()
{
int n, m, sx, sy, tx, ty;
cin >> n >> m;
int d[maxn][maxn];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
char a[maxn][maxn];
queue<int> Q;
    for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
            d[i][j]=inf;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			if (a[i][j] == 'S') {
				sx = i;
				sy = j;
			}
			if (a[i][j] == 'E') {
				tx = i;
				ty = j;
			}
		}
	d[tx][ty] = 0;
	Q.push(tx); Q.push(ty);
	while (! Q.empty()) {
		int x = Q.front(); Q.pop();
		int y = Q.front(); Q.pop();
		for (int i = 0; i < 4; i++) {
			int x1 = x+dx[i];
			int y1 = y+dy[i];
			if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && a[x1][y1] != 'T' && d[x][y]+1 < d[x1][y1]) {
				d[x1][y1] = d[x][y]+1;
				Q.push(x1); Q.push(y1);
			}
		}
	}
	int res = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) if (a[i][j] >= '1' && a[i][j] <= '9') {
			if (d[i][j] <= d[sx][sy])
				res += a[i][j]-'0';
		}
	cout << res << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,n,i=0;

    cin>>n;
    cin.ignore();

    for(i=1;i<=n;i++)
    {
        int c=0;
        char k[100];

        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
             c++;

        }
        if(c>10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<=10;
            cout<<k<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,n,i=0;

    cin>>n;
    cin.ignore();

    for(i=1;i<=n;i++)
    {
        int c=0;
        char k[100];

        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
             c++;

        }
        if(c>=10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
    return 0;
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int j,n,i,c=0;

    cin>>n;
    for(i=0;i<=n;i++)
    {
        char k[100];
        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
            {
                c++;
            }
        }
        if(c>=10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int j,n,i,c=0;
    char k[100];
    cin>>n;
    for(i=0;i<=(n-1);i++)
    {
        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
            {
                c++;
            }
        }
        if(c>=10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int j,n,i,c=0;

    cin>>n;
    for(i=1;i<=n+1;i++)
    {
        char k[100];
        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
            {
                c++;
            }
        }
        if(c>=10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int j,n,i,c=0;
    char k[100];
    cin>>n;
    for(i=0;i<=(n-1);i++)
    {
        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
            {
                c++;
            }
        }
        if(c>10)
        {
            cout<<k[0]<<c-2<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int j,n,i,c=0;
    char k[100];
    cin>>n;
    for(i=0;i<=(n-1);i++)
    {
        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
            {
                c++;
            }
        }
        if(c>10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,n,i=0;

    cin>>n;
    cin.ignore();

    for(i=1;i<=n;i++)
    {
        int c=0;
        char k[100];

        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
             c++;

        }
        if(c>=10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,n,i=0;

    cin>>n;
    cin.ignore();

    for(i=1;i<=n;i++)
    {
        int c=0;
        char k[100];

        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
             c++;

        }
        if(c>10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<=10;
            cout<<k<<endl;

    }
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,n,i=0;

    cin>>n;
    cin.ignore();

    for(i=1;i<=n;i++)
    {
        int c=0;
        char k[100];

        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
             c++;

        }
        if(c>=10)
        {
            cout<<k[0]<<(c-2)<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k<<endl;

    }
return 0;
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int j,n,i,c=0;
    char k[100];
    cin>>n;
    for(i=0;i<=n;i++)
    {
        gets(k);
        {
            for(j=0;k[j]!='\0';j++)
            {
                c++;
            }
        }
        if(c>10)
        {
            cout<<k[0]<<c-2<<k[(c-1)]<<endl;
        }
        if(c<10)
            cout<<k;

    }
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<n;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        s1.push_back(n-2+'0');
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<v.size();i++)
    {
        cout << v[i]<< endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<n;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        s1.push_back(n+'0');
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<n;i++)
    {
        cout << v[i]<< endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<m;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        while(n-2)
        {
           s1.push_back(((n-2)%10)+'0');
           (n-2)/=10;
        }
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<v.size();i++)
    {
        cout << v[i]<< endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    string s;
    cin >> s;
    int n=s.size();
    if(n<=10)cout << s;
    else
    {
        cout << s[0] << n << s[n-1];
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
 int n;
 cin >> n;
  vector<string> v;
  for(int i=0;i<n;i++)
{
 string s;
cin >> s;
v.push_back(s);
}
for(int i=0;i<n;i++)
{
 if(v[i].size>=10) cout <<v[i][0]<<v[i].size()<<v[i][v[i].size()-1]<< endl;
else
{
cout << v[i];
}
}
return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<m;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        s1.push_back((n-2)+'0');
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<v.size();i++)
    {
        cout << v[i]<< endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<m;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        char c=n-2;
        s1.push_back(c);
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<v.size();i++)
    {
        cout << v[i]<< endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<m;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        s1.push_back(n-2+'0');
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<v.size();i++)
    {
        cout << v[i]<< endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,m;
    cin >> m;
    while(m)
    {
       string s;
    cin >> s;
     n=s.size();
    if(n<=10)cout << s<< endl;
    else
    {
        cout << s[0] << n-2 << s[n-1]<< endl;
    }
    m--;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<n;i++)
    {
        cin >> s;
    int n=s.size();
    if(n<=10)cout << v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        s1.push_back(n+'0');
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();

    }
    }
    for(int i=0;i<n;i++)
    {
        cout << v[i];
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,m;
    cin >> m;
    while(m)
    {
       string s;
    cin >> s;
     n=s.size();
    if(n<=10)cout << s;
    else
    {
        cout << s[0] << n << s[n-1]<< endl;
    }
    m--;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s,s1;
    int n,m;
    cin >> m;
    vector<string> v;
    for(int i=0;i<n;i++)
    {
        cin >> s;
     n=s.size();
    if(n<=10)v.push_back(s);
    else
    {
        s1.push_back(s[0]);
        s1.push_back(n+'0');
        s1.push_back(s[n-1]);
        v.push_back(s1);
        s1.clear();
    }
    }
    for(int i=0;i<n;i++)
    {
        cout << v[i];
    }
    return 0;
}