#include<bits/stdc++.h>
#define pii pair<int, int>
#define N 1010
using namespace std;
struct ar
{
    int value, x, y;
};

int a[N][N], d[N][N], v[N][N];;
int row, column;

int x[] = {1, -1, 0, 0};
int y[] = {0, 0, 1, -1};

bool check(int r, int c)
{
    return (r >= 0) && (r < row) && (c >= 0) && (c < column);
}

int bfs(int start_x, int start_y, int finish_x, int finish_y)
{
    int i, j;

    for(i=0;i<1001;i++)
    {
        for(j=0;j<1001;j++)
        {
            d[i][j] = v[i][j] = 0;
        }
    }

    v[start_x][start_y] = 1;

    queue<pii>q;
    q.push(pii(start_x, start_y));

    while(!q.empty())
    {
        pii r = q.front();
        q.pop();

        if(r.first == finish_x && r.second == finish_y)
            return d[finish_x][finish_y];

        for(i=0;i<4;i++)
        {
            int tx = r.first + x[i];
            int ty = r.second + y[i];

            if(check(tx, ty) && a[tx][ty] == 1 && v[tx][ty] == 0)
            {
                v[tx][ty] = 1;
                d[tx][ty] = d[r.first][r.second] + 1;
                q.push(pii(tx, ty));
            }
        }
    }

    return -1;
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> row >> column;

    char ch;
    vector<ar>s;
    int sx, sy, fx, fy;

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
        {
            cin >> ch;
            if(ch == '0')
                a[i][j] = 1;
            else if(ch >= '1' && ch <= '9')
            {
                a[i][j] = 1;
                int p = ch - '0';
                s.push_back({p, i, j});
            }
            else if(ch == 'T')
                a[i][j] = 0;
            else if(ch == 'S')
            {
                a[i][j] = 1;
                sx = i;
                sy = j;
            }
            else if(ch == 'E')
            {
                a[i][j] = 1;
                fx = i;
                fy = j;
            }
        }
    }

    int dis = bfs(sx, sy, fx, fy), c = 0;

    for(int i=0; i<s.size(); i++)
    {
        int d1 = s[i].value;
        int sx1 = s[i].x;
        int sy1 = s[i].y;

        int t = bfs(sx1, sy1, fx, fy);
        if(t != -1)
        {
            if(t <= dis)
            {
                c += d1;
            }
        }
    }

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


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define F first
#define S second

int32_t main(){
   IOS;

   int n,m;
   cin>>n>>m;

   vector <string> v(n);
   for(int i = 0;i<n;i++) cin>>v[i];

   pair <int,int> start,finish;
   for(int i = 0;i<n;i++){
      for(int j = 0;j<m;j++){
         if(v[i][j]=='E'){
            finish.F = i;
            finish.S = j;
         }

         if(v[i][j]=='S'){
            start.F = i;
            start.S = j;
         }
      }
   }

   vector <vector<int>> dist(n,vector<int>(m,0));
   vector <vector<int>> vis(n,vector<int>(m,0));

   queue <pair<int,int>> q;

   q.push(finish);

   while(!q.empty()){
      pair <int,int> a = q.front();
      q.pop();
      int i = a.F;
      int j = a.S;
      vis[i][j]++;

      if(i+1<n && vis[i+1][j]==0 && v[i+1][j]!='T'){
         vis[i+1][j]++;
         q.push({i+1,j});
         dist[i+1][j] = dist[i][j] + 1;
      }

      if(i-1>=0 && vis[i-1][j]==0 && v[i-1][j]!='T'){
         vis[i-1][j]++;
         q.push({i-1,j});
         dist[i-1][j] = dist[i][j] + 1;
      }

      if(j+1<m && vis[i][j+1]==0 && v[i][j+1]!='T'){
         vis[i][j+1]++;
         q.push({i,j+1});
         dist[i][j+1] = dist[i][j] + 1;
      }

      if(j-1>=0 && vis[i][j-1]==0 && v[i][j-1]!='T'){
         vis[i][j-1]++;
         q.push({i,j-1});
         dist[i][j-1] = dist[i][j] + 1;
      }
   }

   int distance = dist[start.F][start.S];

   int ans = 0;

   for(int i = 0;i<n;i++){
      for(int j = 0;j<m;j++){
         if(v[i][j]>='1' && v[i][j]<='9'){
            if(dist[i][j]<=distance && dist[i][j]) ans+= (v[i][j]-'0');
         }
      }
   }

   cout<<ans;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define F first
#define S second

int32_t main(){
   IOS;

   int n,m;
   cin>>n>>m;

   vector <string> v(n);
   for(int i = 0;i<n;i++) cin>>v[i];

   pair <int,int> start,finish;
   for(int i = 0;i<n;i++){
      for(int j = 0;j<m;j++){
         if(v[i][j]=='E'){
            finish.F = i;
            finish.S = j;
         }

         if(v[i][j]=='S'){
            start.F = i;
            start.S = j;
         }
      }
   }

   vector <vector<int>> dist(n,vector<int>(m,0));

   queue <pair<int,int>> q;
   map <pair<int,int>,int> vis;

   q.push(finish);

   while(!q.empty()){
      pair <int,int> a = q.front();
      q.pop();
      vis[a]++;

      int i = a.F;
      int j = a.S;

      if(i+1<n && vis[{i+1,j}]==0 && v[i+1][j]!='T'){
         vis[{i+1,j}]++;
         q.push({i+1,j});
         dist[i+1][j] = dist[i][j] + 1;
      }

      if(i-1>=0 && vis[{i-1,j}]==0 && v[i-1][j]!='T'){
         vis[{i-1,j}]++;
         q.push({i-1,j});
         dist[i-1][j] = dist[i][j] + 1;
      }

      if(j+1<m && vis[{i,j+1}]==0 && v[i][j+1]!='T'){
         vis[{i,j+1}]++;
         q.push({i,j+1});
         dist[i][j+1] = dist[i][j] + 1;
      }

      if(j-1>=0 && vis[{i,j-1}]==0 && v[i][j-1]!='T'){
         vis[{i,j-1}]++;
         q.push({i,j-1});
         dist[i][j-1] = dist[i][j] + 1;
      }
   }

   int distance = dist[start.F][start.S];

   int ans = 0;

   for(int i = 0;i<n;i++){
      for(int j = 0;j<m;j++){
         if(v[i][j]>='1' && v[i][j]<='9'){
            if(dist[i][j]<=distance && dist[i][j]) ans+= (v[i][j]-'0');
         }
      }
   }

   cout<<ans;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define F first
#define S second

int32_t main(){
   IOS;

   int n,m;
   cin>>n>>m;

   vector <string> v(n);
   for(int i = 0;i<n;i++) cin>>v[i];

   pair <int,int> start,finish;
   for(int i = 0;i<n;i++){
      for(int j = 0;j<m;j++){
         if(v[i][j]=='E'){
            finish.F = i;
            finish.S = j;
         }

         if(v[i][j]=='S'){
            start.F = i;
            start.S = j;
         }
      }
   }

   vector <vector<int>> dist(n,vector<int>(m,0));

   queue <pair<int,int>> q;
   map <pair<int,int>,int> vis;

   q.push(finish);

   while(!q.empty()){
      pair <int,int> a = q.front();
      q.pop();
      vis[a]++;

      int i = a.F;
      int j = a.S;

      if(i+1<n && vis[{i+1,j}]==0 && v[i+1][j]!='T'){
         vis[{i+1,j}]++;
         q.push({i+1,j});
         dist[i+1][j] = dist[i][j] + 1;
      }

      if(i-1>=0 && vis[{i-1,j}]==0 && v[i-1][j]!='T'){
         vis[{i-1,j}]++;
         q.push({i-1,j});
         dist[i-1][j] = dist[i][j] + 1;
      }

      if(j+1<m && vis[{i,j+1}]==0 && v[i][j+1]!='T'){
         vis[{i,j+1}]++;
         q.push({i,j+1});
         dist[i][j+1] = dist[i][j] + 1;
      }

      if(j-1>=0 && vis[{i,j-1}]==0 && v[i][j-1]!='T'){
         vis[{i,j-1}]++;
         q.push({i,j-1});
         dist[i][j-1] = dist[i][j] + 1;
      }
   }

   int distance = dist[start.F][start.S];

   int ans = 0;

   for(int i = 0;i<n;i++){
      for(int j = 0;j<m;j++){
         if(v[i][j]>='1' && v[i][j]<='9'){
            if(dist[i][j]<=distance) ans+= (v[i][j]-'0');
         }
      }
   }

   cout<<ans;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define df(b, e) ((b) > (e))
#define fore(i, b, e) for(auto i = (b) - df(b, e); i != e - df(b, e); i += 1 - 2 * df(b, e))
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define ENDL '\n'
typedef long long lli;
typedef long double ld;
typedef pair<int, int> ii;

template <class T>
struct rge { T b, e; };

template <class T>
rge<T> range(T i, T j) {
  return rge<T>{i, j};
}

template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
  ~debug() { cerr << endl; }

  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator << (T i) {
    cerr << boolalpha << i;
    return *this;
  }

  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator << (T i) {
    return *this << range(begin(i), end(i));
  }

  template <class A, class B>
  debug &operator << (pair<A, B> d) {
    return *this << "(" << d.f << ", " << d.s << ")";
  }

  template <class T>
  debug &operator << (rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};

#define deb(...) " " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " "

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define qrank order_of_key // Number of items < x  ---               qrank
#define qkth find_by_order // ith largest element (0-indexed)       qkth

const lli inf = 1e18;
const int N = 1e3 + 5;

char grid[N][N], c;
int vis[N][N];

int dx[4]{ +1, -1, +0, +0};
int dy[4]{ +0, +0, +1, -1};
int n, m;

bool valid(int i, int j) {
    return 0 <= i && i < n && 0 <= j && j < m;
}

struct cell {
    int i, j, t;
};

queue<cell> q;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //ordered_set<pii> st;
    int x, y, i, j;
    cin >> n >> m;
    fore (i, 0, n) {
        fore (j, 0, m) {
            cin >> c;
            grid[i][j] = c;
            if (c == 'E') {
                q.push({i, j, 1});
            }
            if (c == 'S') {
                x = i;
                y = j;
            }
        }
    }
    cell aux = q.front();
    vis[aux.i][aux.j] = 1;
    while (!q.empty()) {
        aux = q.front();
        q.pop();
        fore (k, 0, 4) {
            i = dx[k] + aux.i;
            j = dy[k] + aux.j;
            if (!valid(i, j)) continue;
            if (grid[i][j] == 'T') continue;
            if (vis[i][j]) continue;
            vis[i][j] = aux.t + 1;
            q.push({i, j, aux.t + 1});
        }
    }
    int ans = 0;
    fore (i, 0, n) {
        fore (j, 0, m) {
            if ('0' <= grid[i][j] && grid[i][j] <= '9' && vis[i][j] <= vis[x][y])
                ans += grid[i][j] - '0';
        }
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define df(b, e) ((b) > (e))
#define fore(i, b, e) for(auto i = (b) - df(b, e); i != e - df(b, e); i += 1 - 2 * df(b, e))
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define ENDL '\n'
typedef long long lli;
typedef long double ld;
typedef pair<int, int> ii;

template <class T>
struct rge { T b, e; };

template <class T>
rge<T> range(T i, T j) {
  return rge<T>{i, j};
}

template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
  ~debug() { cerr << endl; }

  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator << (T i) {
    cerr << boolalpha << i;
    return *this;
  }

  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator << (T i) {
    return *this << range(begin(i), end(i));
  }

  template <class A, class B>
  debug &operator << (pair<A, B> d) {
    return *this << "(" << d.f << ", " << d.s << ")";
  }

  template <class T>
  debug &operator << (rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};

#define deb(...) " " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " "

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define qrank order_of_key // Number of items < x  ---               qrank
#define qkth find_by_order // ith largest element (0-indexed)       qkth

const lli inf = 1e18;
const int N = 1e3 + 5;

char grid[N][N], c;
int vis[N][N];

int dx[4]{ +1, -1, +0, +0};
int dy[4]{ +0, +0, +1, -1};
int n, m;

bool valid(int i, int j) {
    return 0 <= i && i < n && 0 <= j && j < m;
}

struct cell {
    int i, j, t;
};

queue<cell> q;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //ordered_set<pii> st;
    int x, y, i, j;
    cin >> n >> m;
    fore (i, 0, n) {
        fore (j, 0, m) {
            cin >> c;
            grid[i][j] = c;
            if (c == 'E') {
                q.push({i, j, 1});
            }
            if (c == 'S') {
                x = i;
                y = j;
            }
        }
    }
    cell aux = q.front();
    vis[aux.i][aux.j] = 1;
    while (!q.empty()) {
        aux = q.front();
        q.pop();
        fore (k, 0, 4) {
            i = dx[k] + aux.i;
            j = dy[k] + aux.j;
            if (!valid(i, j)) continue;
            if (grid[i][j] == 'T') continue;
            if (vis[i][j]) continue;
            vis[i][j] = aux.t + 1;
            q.push({i, j, aux.t + 1});
        }
    }
    int ans = 0;
    fore (i, 0, n) {
        fore (j, 0, m) {
            if ('0' <= grid[i][j] && grid[i][j] <= '9' && vis[i][j] != 0 && vis[i][j] <= vis[x][y])
                ans += grid[i][j] - '0';
        }
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define df(b, e) ((b) > (e))
#define fore(i, b, e) for(auto i = (b) - df(b, e); i != e - df(b, e); i += 1 - 2 * df(b, e))
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define ENDL '\n'
typedef long long lli;
typedef long double ld;
typedef pair<int, int> ii;

template <class T>
struct rge { T b, e; };

template <class T>
rge<T> range(T i, T j) {
  return rge<T>{i, j};
}

template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
  ~debug() { cerr << endl; }

  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator << (T i) {
    cerr << boolalpha << i;
    return *this;
  }

  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator << (T i) {
    return *this << range(begin(i), end(i));
  }

  template <class A, class B>
  debug &operator << (pair<A, B> d) {
    return *this << "(" << d.f << ", " << d.s << ")";
  }

  template <class T>
  debug &operator << (rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};

#define deb(...) " " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " "

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define qrank order_of_key // Number of items < x  ---               qrank
#define qkth find_by_order // ith largest element (0-indexed)       qkth

const lli inf = 1e18;
const int N = 1e3 + 5;

char grid[N][N], c;
int vis[N][N];

int dx[4]{ +1, -1, +0, +0};
int dy[4]{ +0, +0, +1, -1};
int n, m;

bool valid(int i, int j) {
    return 0 <= i && i < n && 0 <= j && j < m;
}

struct data {
    int i, j, t;
};

queue<data> q;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //ordered_set<pii> st;
    int x, y, i, j;
    cin >> n >> m;
    fore (i, 0, n) {
        fore (j, 0, m) {
            cin >> c;
            grid[i][j] = c;
            if (c == 'E') {
                q.push({i, j, 1});
            }
            if (c == 'S') {
                x = i;
                y = j;
            }
        }
    }
    data aux = q.front();
    vis[aux.i][aux.j] = 1;
    while (!q.empty()) {
        aux = q.front();
        q.pop();
        fore (k, 0, 4) {
            i = dx[k] + aux.i;
            j = dy[k] + aux.j;
            if (!valid(i, j)) continue;
            if (grid[i][j] == 'T') continue;
            if (vis[i][j]) continue;
            vis[i][j] = aux.t + 1;
            q.push({i, j, aux.t + 1});
        }
    }
    int ans = 0;
    fore (i, 0, n) {
        fore (j, 0, m) {
            if ('0' <= grid[i][j] && grid[i][j] <= '9' && vis[i][j] <= vis[x][y])
                ans += grid[i][j] - '0';
        }
    }
    cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
long long inp[1000][1000];
long long cntt[1000][1000];
long long c,r;
long long vs[1000][1000];
queue<pair<pair<long long , long long> , long long > >  BFS;
void bfs(long long x, long long y, long long cnt)
{
	if(inp[x][y]==-1||x<0||y<0||x>=r||y>=c||vs[x][y]==1)
	{
		BFS.pop();
		return;
	}
	vs[x][y]=1;
	cntt[x][y]=cnt;
	if(!BFS.empty())
	{
		BFS.pop();
	}
	BFS.push({{x+1,y},cnt+1});
	BFS.push({{x-1,y},cnt+1});
	BFS.push({{x,y+1},cnt+1});
	BFS.push({{x,y-1},cnt+1});
	while(!BFS.empty())
	{
		auto tt=BFS.front();
		bfs(tt.fi.fi, tt.fi.se, tt.se);
	}
}
signed main()
{
	cin >> r >> c;
	memset(inp,0,sizeof(inp));
	memset(cntt,0,sizeof(cntt));
	memset(vs,0,sizeof(vs));
	long long xx,yy,x2,y2;
	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
		{
			char a;
			cin >> a;
			if(a=='T')
			{
				inp[i][j]=-1;
			}
			else if(a=='S')
			{
				xx=i;
				yy=j;
			}
			else if(a=='E')
			{
				x2=i;
				y2=j;
			}
			else
			{
				int l=(int) a-'0';
				inp[i][j]=l;
			}
		}
	}
	bfs(x2,y2,1);
	long long check=cntt[xx][yy];
	//cout << check;
	long long ans=0;
	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
		{
		//	cout<< inp[i][j]<<" ";
			if(inp[i][j]>0&&cntt[i][j]<=check&&cntt[i][j]!=0)
			{
				ans+=inp[i][j];
			}
		}
	//	cout << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
long long inp[1000][1000];
long long cntt[1000][1000];
long long c,r;
long long vs[1000][1000];
vector<pair<pair<long long , long long> , long long > >  BFS;
void bfs(long long x, long long y, long long cnt)
{
	if(inp[x][y]==-1||x<0||y<0||x>=r||y>=c||vs[x][y]==1)
	{
		BFS.erase(BFS.begin());
		return;
	}
	vs[x][y]=1;
	cntt[x][y]=cnt;
	if(!BFS.empty())
	{
		BFS.erase(BFS.begin());
	}
	BFS.push_back({{x+1,y},cnt+1});
	BFS.push_back({{x-1,y},cnt+1});
	BFS.push_back({{x,y+1},cnt+1});
	BFS.push_back({{x,y-1},cnt+1});
	while(!BFS.empty())
	{
		auto tt=BFS[0];
		bfs(tt.fi.fi, tt.fi.se, tt.se);
	}
}
signed main()
{
	cin >> r >> c;
	memset(inp,0,sizeof(inp));
	memset(cntt,0,sizeof(cntt));
	memset(vs,0,sizeof(vs));
	long long xx,yy,x2,y2;
	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
		{
			char a;
			cin >> a;
			if(a=='T')
			{
				inp[i][j]=-1;
			}
			else if(a=='S')
			{
				xx=i;
				yy=j;
			}
			else if(a=='E')
			{
				x2=i;
				y2=j;
			}
			else
			{
				int l=(int) a-'0';
				inp[i][j]=l;
			}
		}
	}
	bfs(x2,y2,1);
	long long check=cntt[xx][yy];
	//cout << check;
	long long ans=0;
	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
		{
			//cout<< cntt[i][j]<<" ";
			if(inp[i][j]>0&&cntt[i][j]<=check)
			{
				ans+=inp[i][j];
			}
		}
		//cout << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
long long inp[1000][1000];
long long cntt[1000][1000];
long long c,r;
long long vs[1000][1000];
vector<pair<pair<long long , long long> , long long > >  BFS;
void bfs(long long x, long long y, long long cnt)
{
	if(inp[x][y]==-1||x<0||y<0||x>=r||y>=c||vs[x][y]==1)
	{
		BFS.erase(BFS.begin());
		return;
	}
	vs[x][y]=1;
	cntt[x][y]=cnt;
	if(!BFS.empty())
	{
		BFS.erase(BFS.begin());
	}
	BFS.push_back({{x+1,y},cnt+1});
	BFS.push_back({{x-1,y},cnt+1});
	BFS.push_back({{x,y+1},cnt+1});
	BFS.push_back({{x,y-1},cnt+1});
	while(!BFS.empty())
	{
		auto tt=BFS[0];
		bfs(tt.fi.fi, tt.fi.se, tt.se);
	}
}
signed main()
{
	cin >> r >> c;
	memset(inp,0,sizeof(inp));
	memset(cntt,0,sizeof(cntt));
	memset(vs,0,sizeof(vs));
	long long xx,yy,x2,y2;
	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
		{
			char a;
			cin >> a;
			if(a=='T')
			{
				inp[i][j]=-1;
			}
			else if(a=='S')
			{
				xx=i;
				yy=j;
			}
			else if(a=='E')
			{
				x2=i;
				y2=j;
			}
			else
			{
				int l=(int) a-'0';
				inp[i][j]=l;
			}
		}
	}
	bfs(x2,y2,1);
	long long check=cntt[xx][yy];
	//cout << check;
	long long ans=0;
	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
		{
			//cout<< cntt[i][j]<<" ";
			if(inp[i][j]>0&&cntt[i][j]<=check&&cntt[i][j]!=0)
			{
				ans+=inp[i][j];
			}
		}
		//cout << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define int long long int
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

template<class T1,class T2> ostream& operator << (ostream &o,pair<T1,T2>x) {return o<<x.fi<<" "<<x.se;}
template<class T1,class T2> istream& operator >> (istream &o,pair<T1,T2>&x) {return o>>x.fi>>x.se;}
template<class T1,class T2> pair<T1,T2> operator + (pair<T1,T2>a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se; return a;}
template<class T1,class T2> pair<T1,T2> operator - (pair<T1,T2>a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se; return a;}
template<class T1,class T2> void operator += (pair<T1,T2>&a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se;}
template<class T1,class T2> void operator -= (pair<T1,T2>&a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se;}

template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {
  }
};

void solve(){
	int r,c,i,j,sx,sy,ex,ey;
	cin>>r>>c;

	string forest[r];

	for(i=0;i<r;i++)
		cin>>forest[i];

	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(forest[i][j]=='S'){
				sx=i;
				sy=j;
			}
			if(forest[i][j]=='E'){
				ex=i;
				ey=j;
			}
		}
	}

	vector<vector<int>> distance(r,vector<int> (c,0));
	queue<pair<int,int>> q;
	distance[ex][ey]=1;

	q.push(mp(ex,ey));
	while(!q.empty()){
		int x=q.front().fi,y=q.front().se;
		q.pop();

		if(x-1>=0){
			if(forest[x-1][y]!='T' && distance[x-1][y]==0){
				q.push(mp(x-1,y));
				distance[x-1][y]=distance[x][y]+1;
			}
		}
		if(x+1<r){
			if(forest[x+1][y]!='T' && distance[x+1][y]==0){
				q.push(mp(x+1,y));
				distance[x+1][y]=distance[x][y]+1;
			}
		}
		if(y-1>=0){
			if(forest[x][y-1]!='T' && distance[x][y-1]==0){
				q.push(mp(x,y-1));
				distance[x][y-1]=distance[x][y]+1;
			}
		}
		if(y+1<c){
			if(forest[x][y+1]!='T' && distance[x][y+1]==0){
				q.push(mp(x,y+1));
				distance[x][y+1]=distance[x][y]+1;
			}
		}
	}

	int a=distance[sx][sy],battles=0;

/*	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			cout<<distance[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
*/
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(isdigit(forest[i][j])){
				if(distance[i][j]<=a && distance[i][j]>1)
					battles+=forest[i][j]-'0';
			}
		}
	}

	cout<<battles<<endl;
}

int32_t main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int T;
	//cin>>T;
	T=1;
	while(T--){
		solve();
	}
}


#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define int long long int
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

template<class T1,class T2> ostream& operator << (ostream &o,pair<T1,T2>x) {return o<<x.fi<<" "<<x.se;}
template<class T1,class T2> istream& operator >> (istream &o,pair<T1,T2>&x) {return o>>x.fi>>x.se;}
template<class T1,class T2> pair<T1,T2> operator + (pair<T1,T2>a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se; return a;}
template<class T1,class T2> pair<T1,T2> operator - (pair<T1,T2>a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se; return a;}
template<class T1,class T2> void operator += (pair<T1,T2>&a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se;}
template<class T1,class T2> void operator -= (pair<T1,T2>&a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se;}

template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {
  }
};

void solve(){
	int r,c,i,j,sx,sy,ex,ey;
	cin>>r>>c;

	string forest[r];

	for(i=0;i<r;i++)
		cin>>forest[i];

	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(forest[i][j]=='S'){
				sx=i;
				sy=j;
			}
			if(forest[i][j]=='E'){
				ex=i;
				ey=j;
			}
		}
	}

	vector<vector<int>> distance(r,vector<int> (c,0));
	queue<pair<int,int>> q;
	distance[ex][ey]=1;

	q.push(mp(ex,ey));
	while(!q.empty()){
		int x=q.front().fi,y=q.front().se;
		q.pop();

		if(x-1>=0){
			if(forest[x-1][y]!='T' && distance[x-1][y]==0){
				q.push(mp(x-1,y));
				distance[x-1][y]=distance[x][y]+1;
			}
		}
		if(x+1<r){
			if(forest[x+1][y]!='T' && distance[x+1][y]==0){
				q.push(mp(x+1,y));
				distance[x+1][y]=distance[x][y]+1;
			}
		}
		if(y-1>=0){
			if(forest[x][y-1]!='T' && distance[x][y-1]==0){
				q.push(mp(x,y-1));
				distance[x][y-1]=distance[x][y]+1;
			}
		}
		if(y+1<c){
			if(forest[x][y+1]!='T' && distance[x][y+1]==0){
				q.push(mp(x,y+1));
				distance[x][y+1]=distance[x][y]+1;
			}
		}
	}

	int a=distance[sx][sy],battles=0;
/*
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			cout<<distance[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
*/
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(isdigit(forest[i][j])){
				if(distance[i][j]<=a)
					battles+=forest[i][j]-'0';
			}
		}
	}

	cout<<battles<<endl;
}

int32_t main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int T;
	//cin>>T;
	T=1;
	while(T--){
		solve();
	}
}


#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define int long long int
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

template<class T1,class T2> ostream& operator << (ostream &o,pair<T1,T2>x) {return o<<x.fi<<" "<<x.se;}
template<class T1,class T2> istream& operator >> (istream &o,pair<T1,T2>&x) {return o>>x.fi>>x.se;}
template<class T1,class T2> pair<T1,T2> operator + (pair<T1,T2>a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se; return a;}
template<class T1,class T2> pair<T1,T2> operator - (pair<T1,T2>a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se; return a;}
template<class T1,class T2> void operator += (pair<T1,T2>&a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se;}
template<class T1,class T2> void operator -= (pair<T1,T2>&a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se;}

template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {
  }
};

void solve(){
	int r,c,i,j,sx,sy,ex,ey;
	cin>>r>>c;

	string forest[r];

	for(i=0;i<r;i++)
		cin>>forest[i];

	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(forest[i][j]=='S'){
				sx=i;
				sy=j;
			}
			if(forest[i][j]=='E'){
				ex=i;
				ey=j;
			}
		}
	}

	vector<vector<int>> distance(r,vector<int> (c,0));
	queue<pair<int,int>> q;

	q.push(mp(ex,ey));
	while(!q.empty()){
		int x=q.front().fi,y=q.front().se;
		q.pop();

		if(x-1>=0){
			if(forest[x-1][y]!='T' && distance[x-1][y]==0){
				q.push(mp(x-1,y));
				distance[x-1][y]=distance[x][y]+1;
			}
		}
		if(x+1<r){
			if(forest[x+1][y]!='T' && distance[x+1][y]==0){
				q.push(mp(x+1,y));
				distance[x+1][y]=distance[x][y]+1;
			}
		}
		if(y-1>=0){
			if(forest[x][y]!='T' && distance[x][y-1]==0){
				q.push(mp(x,y-1));
				distance[x][y-1]=distance[x][y]+1;
			}
		}
		if(y+1<c){
			if(forest[x][y+1]!='T' && distance[x][y+1]==0){
				q.push(mp(x,y+1));
				distance[x][y+1]=distance[x][y]+1;
			}
		}
	}

	int a=distance[sx][sy],battles=0;

	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(isdigit(forest[i][j])){
				if(distance[i][j]<=a)
					battles+=forest[i][j]-'0';
			}
		}
	}

	cout<<battles<<endl;
}

int32_t main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int T;
	//cin>>T;
	T=1;
	while(T--){
		solve();
	}
}


#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define int long long int
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

template<class T1,class T2> ostream& operator << (ostream &o,pair<T1,T2>x) {return o<<x.fi<<" "<<x.se;}
template<class T1,class T2> istream& operator >> (istream &o,pair<T1,T2>&x) {return o>>x.fi>>x.se;}
template<class T1,class T2> pair<T1,T2> operator + (pair<T1,T2>a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se; return a;}
template<class T1,class T2> pair<T1,T2> operator - (pair<T1,T2>a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se; return a;}
template<class T1,class T2> void operator += (pair<T1,T2>&a,pair<T1,T2>b) {a.fi+=b.fi; a.se+=b.se;}
template<class T1,class T2> void operator -= (pair<T1,T2>&a,pair<T1,T2>b) {a.fi-=b.fi; a.se-=b.se;}

template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {
  }
};

void solve(){
	int r,c,i,j,sx,sy,ex,ey;
	cin>>r>>c;

	string forest[r];

	for(i=0;i<r;i++)
		cin>>forest[i];

	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(forest[i][j]=='S'){
				sx=i;
				sy=j;
			}
			if(forest[i][j]=='E'){
				ex=i;
				ey=j;
			}
		}
	}

	vector<vector<int>> distance(r,vector<int> (c,0));
	queue<pair<int,int>> q;

	q.push(mp(ex,ey));
	while(!q.empty()){
		int x=q.front().fi,y=q.front().se;
		q.pop();

		if(x-1>=0){
			if(forest[x-1][y]!='T' && distance[x-1][y]==0){
				q.push(mp(x-1,y));
				distance[x-1][y]=distance[x][y]+1;
			}
		}
		if(x+1<r){
			if(forest[x+1][y]!='T' && distance[x+1][y]==0){
				q.push(mp(x+1,y));
				distance[x+1][y]=distance[x][y]+1;
			}
		}
		if(y-1>=0){
			if(forest[x][y-1]!='T' && distance[x][y-1]==0){
				q.push(mp(x,y-1));
				distance[x][y-1]=distance[x][y]+1;
			}
		}
		if(y+1<c){
			if(forest[x][y+1]!='T' && distance[x][y+1]==0){
				q.push(mp(x,y+1));
				distance[x][y+1]=distance[x][y]+1;
			}
		}
	}

	int a=distance[sx][sy],battles=0;
/*
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			cout<<distance[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
*/
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(isdigit(forest[i][j])){
				if(distance[i][j]<=a)
					battles+=forest[i][j]-'0';
			}
		}
	}

	cout<<battles<<endl;
}

int32_t main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int T;
	//cin>>T;
	T=1;
	while(T--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12

int main()
{
	ll v,x,y,e,a,b,in=0;
	pll h;
	cin>>a>>b;
	char z,m;
	char ch[a][b];
	pair<int,int>r;
	pll init;
	pll fin;
	vector<pll> en;
	map<pair<int,int>,int >w;
	set<pair<int,int> >fg;

	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			cin>>z;
			ch[i][j]=z;
			h.first=i;
			h.second=j;
			if(isdigit(z))
			{

				w[h]=in;
				if(z-48>0)
				{
					r.first=in;
					r.second=z-48;
					en.push_back(r);

				}
				in++;
				fg.insert(h);
			}
			else if(z=='E')
			{
				fin.first=i;
				fin.second=j;
				w[h]=in;
				fg.insert(h);
				in++;
			}
			else if(z=='S')
			{
				init.first=i;
				init.second=j;
                fg.insert(h);
				w[h]=in;

				in++;
			}
		}
	}
	pll au;
	v=w.size();
	vector<vector<pll> > grafo(v,vector<pll> (0));
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			r.first=i;
			r.second=j;
			if(fg.count(r))
			{
				//cout<<w[r]<<endl;

				/*if(ch[i][j]-48>0&&isdigit(ch[i][j]))
				{
					cout<<ch[i][j]<<endl;
					cout<<ch[i-1][j]<<" "<<w[make_pair(i-1,j)]<<" "<<ch[i+1][j]<<" "<<ch[i][j-1]<<" "<<ch[i][j+1]<<endl;
				}*/
				if(i-1>=0)
				{
				    h.first=i-1;
                    h.second=j;
					if(fg.count(h))
					{
						au.first=1;
						au.second=w[h];
						grafo[w[r]].push_back(au);

					}
				}
					if(j-1>=0)
				{
				    h.first=i;
                    h.second=j-1;
					if(fg.count(h))
						{

						au.first=1;
						au.second=w[h];
							grafo[w[r]].push_back(au);
						}
				}
				if(j+1<b)
				{
				    h.first=i;
                    h.second=j+1;
					if(fg.count(h))
                        {
          				au.first=1;
						au.second=w[h];
							grafo[w[r]].push_back(au);
						}
				}
				if(i+1<a)
				{
				    h.first=i+1;
                    h.second=j;
					if(fg.count(h))
					{

						au.first=1;
						au.second=w[h];
						grafo[w[r]].push_back(au);
					}

				}
			}
		}
	}

	vector<ll> peso(v,(ll)INF);
	vl llegue(v,(ll)INF);
	priority_queue<pll,vector<pll>,greater<pll> >q;//comandos para ordenar de menor a mayor
	int inicio,destino;
	inicio=w[fin];
	pll inicial(0,inicio);
	llegue[inicio]=0;
    peso[inicio]=0;
	q.push(inicial);

	while(!q.empty()){

		pll t=q.top();
	    q.pop();
		ll pes=t.first;
		int nodo=t.second;
		for(ll i=0;i<grafo[nodo].size();i++)
		{

		   	if(peso[grafo[nodo][i].second]>pes+grafo[nodo][i].first)
		   	{
		   		peso[grafo[nodo][i].second]=pes+grafo[nodo][i].first;
		   		pll nuevo(pes+grafo[nodo][i].first,grafo[nodo][i].second);
		   		q.push(nuevo);
		   		llegue[grafo[nodo][i].second]=nodo;
			}
	    }
	}
	ll ind=peso[w[init]],sum=0;
	for(int i=0;i<en.size();i++)
	{
		if(peso[en[i].first]<=ind)
		sum+=en[i].second;
	}
	cout<<sum<<endl;

  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j].second<<" ";
	}
	cout<<endl;
  }*/

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
	ll v,x,y,e,a,b,in=0;
	pll h;
	cin>>a>>b;
	char z,m;
	char ch[a][b];
	pair<ll,ll>r;
	pll init;
	pll fin;
	vector<pll> en;
	map<pair<ll,ll>,ll >w;
    int fini,ini;
	for(ll i=0;i<a;i++)
	{
		for(ll j=0;j<b;j++)
		{
			cin>>z;
			ch[i][j]=z;
			h.first=i;
			h.second=j;
			if(isdigit(z))
			{

				w[h]=in;
				if(z-48>0)
				{
					r.first=in;
					r.second=z-48;
					en.push_back(r);

				}
				in++;
			}
			else if(z=='E')
			{
				fin.first=i;
				fin.second=j;
				fini=in;
				w[h]=in;
				in++;
			}
			else if(z=='S')
			{
				init.first=i;
				init.second=j;
				ini=in;
				w[h]=in;

				in++;
			}
		}
	}
	v=w.size();
	vector<vl > grafo(v,vl(0));
	int re;
	for(ll i=0;i<a;i++)
	{
		for(ll j=0;j<b;j++)
		{
			re=w[make_pair(i,j)];
			if(ch[i][j]!='T')
			{
				//cout<<w[r]<<endl;

				/*if(ch[i][j]-48>0&&isdigit(ch[i][j]))
				{
					cout<<ch[i][j]<<endl;
					cout<<ch[i-1][j]<<" "<<w[make_pair(i-1,j)]<<" "<<ch[i+1][j]<<" "<<ch[i][j-1]<<" "<<ch[i][j+1]<<endl;
				}*/
				if(i-1>=0)
				{
				    h.first=i-1;
                    h.second=j;
					if(ch[i-1][j]!='T')
					{
						grafo[re].push_back(w[h]);

					}
				}
					if(j-1>=0)
				{
				    h.first=i;
                    h.second=j-1;
					if(ch[i][j-1]!='T')
						{
							grafo[re].push_back(w[h]);
						}
				}
				if(j+1<b)
				{
				    h.first=i;
                    h.second=j+1;
					if(ch[i][j+1]!='T')
                        {
							grafo[re].push_back(w[h]);
						}
				}
				if(i+1<a)
				{
				    h.first=i+1;
                    h.second=j;
					if(ch[i+1][j]!='T')
					{
						grafo[re].push_back(w[h]);
					}

				}
			}
		}
	}

	vector<bool> visitado(v,false);
    ll dist[v];
		dist[fini]=0;
		if(!visitado[fini]){
		queue<ll> q;
		q.push(w[fin]);
		ll o=0;
		while(!q.empty()){

		    ll t=q.front();
		    q.pop();
		    if(!visitado[t]){

		        visitado[t]=true;
		        for(ll i=0;i<grafo[t].size();i++){

		            if(!visitado[grafo[t][i]])
					{
					    dist[grafo[t][i]]=dist[t]+1;
						q.push(grafo[t][i]);
					}
		        }
		    }
		}
		//u.

		}
    //cout<<dist[w[init]]<<" "<<w[init]<<endl;

	ll ind=dist[ini],sum=0;
	for(ll i=0;i<en.size();i++)
	{
	    //cout<<dist[en[i].first]<<" "<<en[i].first<<endl;
		if(dist[en[i].first]<=ind)
		sum+=en[i].second;
	}
	cout<<sum<<endl;

  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j]<<" ";
	}
	cout<<endl;
  }*/

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
	ll v,x,y,e,a,b,in=0;
	pll h;
	cin>>a>>b;
	char z,m;
	char ch[a][b];
	pair<int,int>r;
	pll init;
	pll fin;
	vector<pll> en;
	map<pair<int,int>,int >w;

	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			cin>>z;
			ch[i][j]=z;
			h.first=i;
			h.second=j;
			if(isdigit(z))
			{

				w[h]=in;
				if(z-48>0)
				{
					r.first=in;
					r.second=z-48;
					en.push_back(r);

				}
				in++;
			}
			else if(z=='E')
			{
				fin.first=i;
				fin.second=j;
				w[h]=in;
				in++;
			}
			else if(z=='S')
			{
				init.first=i;
				init.second=j;
				w[h]=in;

				in++;
			}
		}
	}
	v=w.size();
	vector<vl > grafo(v,vl(0));
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			r.first=i;
			r.second=j;
			if(ch[i][j]!='T')
			{
				//cout<<w[r]<<endl;

				/*if(ch[i][j]-48>0&&isdigit(ch[i][j]))
				{
					cout<<ch[i][j]<<endl;
					cout<<ch[i-1][j]<<" "<<w[make_pair(i-1,j)]<<" "<<ch[i+1][j]<<" "<<ch[i][j-1]<<" "<<ch[i][j+1]<<endl;
				}*/
				if(i-1>=0)
				{
				    h.first=i-1;
                    h.second=j;
					if(ch[i-1][j]!='T')
					{
						grafo[w[r]].push_back(w[h]);

					}
				}
					if(j-1>=0)
				{
				    h.first=i;
                    h.second=j-1;
					if(ch[i][j-1]!='T')
						{
							grafo[w[r]].push_back(w[h]);
						}
				}
				if(j+1<b)
				{
				    h.first=i;
                    h.second=j+1;
					if(ch[i][j+1]!='T')
                        {
							grafo[w[r]].push_back(w[h]);
						}
				}
				if(i+1<a)
				{
				    h.first=i+1;
                    h.second=j;
					if(ch[i+1][j]!='T')
					{
						grafo[w[r]].push_back(w[h]);
					}

				}
			}
		}
	}

	vector<bool> visitado(v,false);
    ll dist[v];
		dist[w[fin]]=0;
		if(!visitado[w[fin]]){
		queue<long long> q;
		q.push(w[fin]);
		long long o=0;
		while(!q.empty()){

		    long long t=q.front();
		    q.pop();
		    if(!visitado[t]){

		        visitado[t]=true;
		        for(long long i=0;i<grafo[t].size();i++){

		            if(!visitado[grafo[t][i]])
					{
					    dist[grafo[t][i]]=dist[t]+1;
						q.push(grafo[t][i]);
					}
		        }
		    }
		}
		//u.

		}
    //cout<<dist[w[init]]<<" "<<w[init]<<endl;

	ll ind=dist[w[init]],sum=0;
	for(int i=0;i<en.size();i++)
	{
	    //cout<<dist[en[i].first]<<" "<<en[i].first<<endl;
		if(dist[en[i].first]<=ind)
		sum+=en[i].second;
	}
	cout<<sum<<endl;

  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j]<<" ";
	}
	cout<<endl;
  }*/

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
	ll v,x,y,e,a,b,in=0;
	pll h;
	cin>>a>>b;
	char z,m;
	char ch[a][b];
	pair<int,int>r;
	pll init;
	pll fin;
	vector<pll> en;
	map<pair<int,int>,int >w;
	set<pair<int,int> >fg;

	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			cin>>z;
			ch[i][j]=z;
			h.first=i;
			h.second=j;
			if(isdigit(z))
			{

				w[h]=in;
				if(z-48>0)
				{
					r.first=in;
					r.second=z-48;
					en.push_back(r);

				}
				in++;
				fg.insert(h);
			}
			else if(z=='E')
			{
				fin.first=i;
				fin.second=j;
				w[h]=in;
				fg.insert(h);
				in++;
			}
			else if(z=='S')
			{
				init.first=i;
				init.second=j;
                fg.insert(h);
				w[h]=in;

				in++;
			}
		}
	}
	v=w.size();
	vector<vl > grafo(v,vl(0));
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			r.first=i;
			r.second=j;
			if(fg.count(r))
			{
				//cout<<w[r]<<endl;

				/*if(ch[i][j]-48>0&&isdigit(ch[i][j]))
				{
					cout<<ch[i][j]<<endl;
					cout<<ch[i-1][j]<<" "<<w[make_pair(i-1,j)]<<" "<<ch[i+1][j]<<" "<<ch[i][j-1]<<" "<<ch[i][j+1]<<endl;
				}*/
				if(i-1>=0)
				{
				    h.first=i-1;
                    h.second=j;
					if(fg.count(h))
					{
						grafo[w[r]].push_back(w[h]);

					}
				}
					if(j-1>=0)
				{
				    h.first=i;
                    h.second=j-1;
					if(fg.count(h))
						{
							grafo[w[r]].push_back(w[h]);
						}
				}
				if(j+1<b)
				{
				    h.first=i;
                    h.second=j+1;
					if(fg.count(h))
                        {
							grafo[w[r]].push_back(w[h]);
						}
				}
				if(i+1<a)
				{
				    h.first=i+1;
                    h.second=j;
					if(fg.count(h))
					{
						grafo[w[r]].push_back(w[h]);
					}

				}
			}
		}
	}

	vector<bool> visitado(v,false);
    ll dist[v];
		dist[w[fin]]=0;
		if(!visitado[w[fin]]){
		queue<long long> q;
		q.push(w[fin]);
		long long o=0;
		while(!q.empty()){

		    long long t=q.front();
		    q.pop();
		    if(!visitado[t]){

		        visitado[t]=true;
		        for(long long i=0;i<grafo[t].size();i++){

		            if(!visitado[grafo[t][i]])
					{
					    dist[grafo[t][i]]=dist[t]+1;
						q.push(grafo[t][i]);
					}
		        }
		    }
		}
		//u.

		}
    //cout<<dist[w[init]]<<" "<<w[init]<<endl;

	ll ind=dist[w[init]],sum=0;
	for(int i=0;i<en.size();i++)
	{
	    //cout<<dist[en[i].first]<<" "<<en[i].first<<endl;
		if(dist[en[i].first]<=ind)
		sum+=en[i].second;
	}
	cout<<sum<<endl;

  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j]<<" ";
	}
	cout<<endl;
  }*/

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12
ll inicio,destino;
pll z;

int main()
{

	ll v,x,y,e,B,P;
	pll h;
	while(cin>>v>>e){
		if(v+e==0)break;
	cin>>destino>>inicio;
	vector<vector<pll> > grafo(v,vector<pll> (0));
	vector<vector<pll> > grafo2(v,vector<pll> (0));
	vector<ll> peso(v,(ll)INF);
	vl llegue(v,(ll)INF);

    ll weight;
	for(ll i=0;i<e;i++)
	{
		cin>>x>>y>>weight;
		h.first=weight;
		h.second=x;
		grafo[y].push_back(h);
		h.second=y;
		grafo2[x].push_back(h);
	}

	priority_queue<pll,vector<pll>,greater<pll> >q;//comandos para ordenar de menor a mayor

	pll inicial(0,inicio);
	llegue[inicio]=0;

	q.push(inicial);
    peso[inicio]=0;
	while(!q.empty()){

		pll t=q.top();
	    q.pop();
		ll pes=t.first;
		ll nodo=t.second;
		for(ll i=0;i<grafo[nodo].size();i++)
		{

		   	if(peso[grafo[nodo][i].second]>pes+grafo[nodo][i].first)
		   	{
		   		peso[grafo[nodo][i].second]=pes+grafo[nodo][i].first;
		   		pll nuevo(pes+grafo[nodo][i].first,grafo[nodo][i].second);
		   		q.push(nuevo);
		   		llegue[grafo[nodo][i].second]=nodo;
			}
	    }
	}
	int maxi=peso[destino];
	//cout<<maxi<<endl;
	vl peso2;

	for(ll i=0;i<v;i++)
	{
		peso2.push_back(peso[i]);
	}

	peso.clear();

	peso.assign(v,(ll)INF);

	inicial.first=0;
	inicial.second=destino;
	q.push(inicial);
    peso[destino]=0;
	while(!q.empty()){

		pll t=q.top();
	    q.pop();
		ll pes=t.first;
		ll nodo=t.second;
		for(ll i=0;i<grafo2[nodo].size();i++)
		{

		   	if(peso[grafo2[nodo][i].second]>pes+grafo2[nodo][i].first)
		   	{
		   		peso[grafo2[nodo][i].second]=pes+grafo2[nodo][i].first;
		   		pll nuevo(pes+grafo2[nodo][i].first,grafo2[nodo][i].second);
		   		q.push(nuevo);
			}
	    }
	}
	//cout<<peso[inicio]<<endl;
	vl peso3;
	for(ll i=0;i<v;i++)
	{
		peso3.push_back(peso[i]);
	}

	peso.clear();
	peso.assign(v,(ll)INF);
	inicial.first=0;
	inicial.second=destino;
	q.push(inicial);
    peso[destino]=0;
	while(!q.empty()){

		pll t=q.top();
	    q.pop();
		ll pes=t.first;
		ll nodo=t.second;
		for(ll i=0;i<grafo2[nodo].size();i++)
		{
		    //cout<<nodo<<" "<<grafo2[nodo][i].second<<" "<<peso3[nodo]<<" "<<grafo2[nodo][i].first<<" "<<peso2[grafo2[nodo][i].second]<<endl;
            if(peso3[nodo]+grafo2[nodo][i].first+peso2[grafo2[nodo][i].second]==maxi)continue;
		   	if(peso[grafo2[nodo][i].second]>pes+grafo2[nodo][i].first)
		   	{
		   		peso[grafo2[nodo][i].second]=pes+grafo2[nodo][i].first;
		   		pll nuevo(pes+grafo2[nodo][i].first,grafo2[nodo][i].second);
		   		q.push(nuevo);
			}
	    }
	}
	if(peso[inicio]==INF)cout<<"-1\n";
	else cout<<peso[inicio]<<endl;
  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j].second<<" ";
	}
	cout<<endl;
  }*/
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
	ll v,x,y,e,a,b,in=0;
	pll h;
	cin>>a>>b;
	char z,m;
	char ch[a][b];
	pair<int,int>r;
	pll init;
	pll fin;
	vector<pll> en;
	map<pair<int,int>,int >w;
	set<pair<int,int> >fg;

	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			cin>>z;
			ch[i][j]=z;
			h.first=i;
			h.second=j;
			if(isdigit(z))
			{

				w[h]=in;
				if(z-48>0)
				{
					r.first=in;
					r.second=z-48;
					en.push_back(r);

				}
				in++;
				fg.insert(h);
			}
			else if(z=='E')
			{
				fin.first=i;
				fin.second=j;
				w[h]=in;
				fg.insert(h);
				in++;
			}
			else if(z=='S')
			{
				init.first=i;
				init.second=j;
                fg.insert(h);
				w[h]=in;

				in++;
			}
		}
	}
	pll au;
	v=w.size();
	vector<vector<pll> > grafo(v,vector<pll> (0));
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			r.first=i;
			r.second=j;
			if(fg.count(r))
			{
				//cout<<w[r]<<endl;

				/*if(ch[i][j]-48>0&&isdigit(ch[i][j]))
				{
					cout<<ch[i][j]<<endl;
					cout<<ch[i-1][j]<<" "<<w[make_pair(i-1,j)]<<" "<<ch[i+1][j]<<" "<<ch[i][j-1]<<" "<<ch[i][j+1]<<endl;
				}*/
				if(i-1>=0)
				{
				    h.first=i-1;
                    h.second=j;
					if(fg.count(h))
					{
						au.first=1;
						au.second=w[h];
						grafo[w[r]].push_back(au);

					}
				}
					if(j-1>=0)
				{
				    h.first=i;
                    h.second=j-1;
					if(fg.count(h))
						{

						au.first=1;
						au.second=w[h];
							grafo[w[r]].push_back(au);
						}
				}
				if(j+1<b)
				{
				    h.first=i;
                    h.second=j+1;
					if(fg.count(h))
                        {
          				au.first=1;
						au.second=w[h];
							grafo[w[r]].push_back(au);
						}
				}
				if(i+1<a)
				{
				    h.first=i+1;
                    h.second=j;
					if(fg.count(h))
					{

						au.first=1;
						au.second=w[h];
						grafo[w[r]].push_back(au);
					}

				}
			}
		}
	}

	vector<ll> peso(v,(ll)INF);
	vl llegue(v,(ll)INF);
	priority_queue<pll,vector<pll>,greater<pll> >q;//comandos para ordenar de menor a mayor
	int inicio,destino;
	inicio=w[fin];
	pll inicial(0,inicio);
	llegue[inicio]=0;
    peso[inicio]=0;
	q.push(inicial);

	while(!q.empty()){

		pll t=q.top();
	    q.pop();
		ll pes=t.first;
		int nodo=t.second;
		for(ll i=0;i<grafo[nodo].size();i++)
		{

		   	if(peso[grafo[nodo][i].second]>pes+grafo[nodo][i].first)
		   	{
		   		peso[grafo[nodo][i].second]=pes+grafo[nodo][i].first;
		   		pll nuevo(pes+grafo[nodo][i].first,grafo[nodo][i].second);
		   		q.push(nuevo);
		   		llegue[grafo[nodo][i].second]=nodo;
			}
	    }
	}
	ll ind=peso[w[init]],sum=0;
	for(int i=0;i<en.size();i++)
	{
		if(peso[en[i].first]<=ind)
		sum+=en[i].second;
	}
	cout<<sum<<endl;

  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j].second<<" ";
	}
	cout<<endl;
  }*/

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define INF 1e12

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
	ll v,x,y,e,a,b,in=0;
	pll h;
	cin>>a>>b;
	char z,m;
	char ch[a][b];
	pair<ll,ll>r;
	pll init;
	pll fin;
	vector<pll> en;
	map<pair<ll,ll>,ll >w;
    int fini,ini;
	for(ll i=0;i<a;i++)
	{
		for(ll j=0;j<b;j++)
		{
			cin>>z;
			ch[i][j]=z;
			h.first=i;
			h.second=j;
			if(isdigit(z))
			{

				w[h]=in;
				if(z-48>0)
				{
					r.first=in;
					r.second=z-48;
					en.push_back(r);

				}
				in++;
			}
			else if(z=='E')
			{
				fin.first=i;
				fin.second=j;
				fini=in;
				w[h]=in;
				in++;
			}
			else if(z=='S')
			{
				init.first=i;
				init.second=j;
				ini=in;
				w[h]=in;

				in++;
			}
		}
	}
	v=w.size();
	vector<vl > grafo(v,vl(0));
	int re;
	for(ll i=0;i<a;i++)
	{
		for(ll j=0;j<b;j++)
		{
			re=w[make_pair(i,j)];
			if(ch[i][j]!='T')
			{
				//cout<<w[r]<<endl;

				/*if(ch[i][j]-48>0&&isdigit(ch[i][j]))
				{
					cout<<ch[i][j]<<endl;
					cout<<ch[i-1][j]<<" "<<w[make_pair(i-1,j)]<<" "<<ch[i+1][j]<<" "<<ch[i][j-1]<<" "<<ch[i][j+1]<<endl;
				}*/
				if(i-1>=0)
				{
				    h.first=i-1;
                    h.second=j;
					if(ch[i-1][j]!='T')
					{
						grafo[re].push_back(w[h]);

					}
				}
					if(j-1>=0)
				{
				    h.first=i;
                    h.second=j-1;
					if(ch[i][j-1]!='T')
						{
							grafo[re].push_back(w[h]);
						}
				}
				if(j+1<b)
				{
				    h.first=i;
                    h.second=j+1;
					if(ch[i][j+1]!='T')
                        {
							grafo[re].push_back(w[h]);
						}
				}
				if(i+1<a)
				{
				    h.first=i+1;
                    h.second=j;
					if(ch[i+1][j]!='T')
					{
						grafo[re].push_back(w[h]);
					}

				}
			}
		}
	}

	vector<bool> visitado(v,false);
    vl dist(v,INF);
		dist[fini]=0;
		if(!visitado[fini]){
		queue<ll> q;
		q.push(fini);
		ll o=0;
		while(!q.empty()){

		    ll t=q.front();
		    q.pop();
		    if(!visitado[t]){

		        visitado[t]=true;
		        for(ll i=0;i<grafo[t].size();i++){

		            if(!visitado[grafo[t][i]])
					{
					    dist[grafo[t][i]]=dist[t]+1;
						q.push(grafo[t][i]);
					}
		        }
		    }
		}
		//u.

		}
    //cout<<dist[w[init]]<<" "<<w[init]<<endl;

	ll ind=dist[ini],sum=0;
	for(ll i=0;i<en.size();i++)
	{
	    //cout<<dist[en[i].first]<<" "<<en[i].first<<endl;
		if(dist[en[i].first]<=ind&&dist[en[i].first]!=INF)
		sum+=en[i].second;
	}
	cout<<sum<<endl;

  /*for(ll i=0;i<grafo.size();i++)
  {
  	cout<<"vecinos de "<<i<<endl;
  	for(ll j=0;j<grafo[i].size();j++)
  	{
  		cout<<grafo[i][j]<<" ";
	}
	cout<<endl;
  }*/

	return 0;
}


#include<bits/stdc++.h>
#define ll  int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define piii pair<ll,pair<ll,ll> >
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
//#define INT_MAXI 9000000000000000000

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

 char vec[1005][1005];
ll r,c;
ll dis[1005][1005];

ll	ei,ej,si,sj,count2=0;
void bfs(ll i,ll j)
{
    ll x1=i,x2=j,count;

	queue<pair<ll,pair<ll,ll> > >q;
	q.push(mp(0,mp(i,j)));

	while(!q.empty())
	{
		auto itr=q.front();
		q.pop();
		ll cost=itr.fi;
		ll x=itr.se.fi;
		ll y=itr.se.se;
		for(ll k=0;k<4;k++)
		{
			ll indx=x+dx[k];
			ll indy=y+dy[k];
			if(indx>=0&&indx<r&&indy>=0&&indy<c&&vec[indx][indy]!='T')
			{
				if(dis[indx][indy]>=cost+1)
				{
					dis[indx][indy]=cost+1;
					q.push(mp(cost+1,mp(indx,indy)));
				}

			}
		}

	}

}

int main()
{
    	fio;
    ll i,j;
    	for(i=0;i<1005;i++)
    	{
    		for(j=0;j<1005;j++)
    		dis[i][j]=INT_MAX;
		}

    cin>>r>>c;

    for(i=0;i<r;i++)
    {
    	for(j=0;j<c;j++)
    	cin>>vec[i][j];
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]=='S')
			{
				si=i;
				sj=j;
			}
			if(vec[i][j]=='E')
			{
				ei=i;
				ej=j;
			}
		}
	}
	bfs(ei,ej);
	ll ets=dis[si][sj];
	ll ans=0;

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]>='1'&&vec[i][j]<='9')
			{
				if(dis[i][j]<=ets)
				ans+=(vec[i][j]-'0');
			}
		}

	}

   cout<<ans;

}


#include<bits/stdc++.h>
#define ll  int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define piii pair<ll,pair<ll,ll> >
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
//#define INT_MAXI 9000000000000000000

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

 char vec[1005][1005];
ll r,c;
ll dis[1005][1005];

ll	ei,ej,si,sj,count2=0;
void bfs(ll i,ll j)
{
    ll x1=i,x2=j,count;

	queue<pair<ll,pair<ll,ll> > >q;
	q.push(mp(0,mp(i,j)));

	while(!q.empty())
	{
		auto itr=q.front();
		q.pop();
		ll cost=itr.fi;
		ll x=itr.se.fi;
		ll y=itr.se.se;
		for(ll k=0;k<4;k++)
		{
			ll indx=x+dx[k];
			ll indy=y+dy[k];
			if(indx>=0&&indx<r&&indy>=0&&indy<c&&vec[indx][indy]!='T'&&dis[i][j]==-1)
			{

					dis[indx][indy]=cost+1;
					q.push(mp(cost+1,mp(indx,indy)));

			}
		}

	}

}

int main()
{
    	fio;
    ll i,j;
    	for(i=0;i<1005;i++)
    	{
    		for(j=0;j<1005;j++)
    		dis[i][j]=-1;
		}

    cin>>r>>c;

    for(i=0;i<r;i++)
    {
    	for(j=0;j<c;j++)
    	cin>>vec[i][j];
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]=='S')
			{
				si=i;
				sj=j;
			}
			if(vec[i][j]=='E')
			{
				ei=i;
				ej=j;
			}
		}
	}
	bfs(ei,ej);
	ll ets=dis[si][sj];
	ll ans=0;

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]>='1'&&vec[i][j]<='9')
			{
				if(dis[i][j]!=-1&&dis[i][j]<=ets)
				ans+=(vec[i][j]-'0');
			}
		}

	}

   cout<<ans;

}


#include<bits/stdc++.h>
#define ll  int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define piii pair<ll,pair<ll,ll> >
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

 vector<string>vec;
ll r,c;
ll dis[1005][1005];

	vii vec2;
ll	ei,ej,si,sj,count2=0;
void bfs(ll i,ll j)
{
    ll x1=i,x2=j,count;

	queue<pair<ll,pair<ll,ll> > >q;
	q.push(mp(0,mp(i,j)));

	while(!q.empty())
	{
		auto itr=q.front();
		q.pop();
		ll cost=itr.fi;
		ll x=itr.se.fi;
		ll y=itr.se.se;
		for(ll k=0;k<4;k++)
		{
			ll indx=x+dx[k];
			ll indy=y+dy[k];
			if(indx>=0&&indx<r&&indy>=0&&indy<c&&vec[indx][indy]!='T')
			{
				if(dis[indx][indy]>=cost+1)
				{
					dis[indx][indy]=cost+1;
					q.push(mp(cost+1,mp(indx,indy)));
				}

			}
		}

	}

}

int main()
{
    	fio;
    ll i,j;
    	for(i=0;i<1005;i++)
    	{
    		for(j=0;j<1005;j++)
    		dis[i][j]=INT_MAX;
		}

    cin>>r>>c;

    for(i=0;i<r;i++)
    {
    	string str;
    	cin>>str;
    	vec.push_back(str);
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]=='S')
			{
				si=i;
				sj=j;
			}
			if(vec[i][j]=='E')
			{
				ei=i;
				ej=j;
			}
		}
	}
	bfs(ei,ej);
	ll ets=dis[si][sj];
	ll ans=0;

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]>='1'&&vec[i][j]<='9')
			{
				if(dis[i][j]<=ets)
				ans+=(vec[i][j]-'0');
			}
		}

	}

   cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define piii pair<ll,pair<ll,ll> >
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

 vector<string>vec;
ll r,c;
ll dis[1005][1005];

	vii vec2;
ll	ei,ej,si,sj,count2=0;
void bfs(ll i,ll j)
{
    ll x1=i,x2=j,count;

	queue<pair<ll,pair<ll,ll> > >q;
	q.push(mp(0,mp(i,j)));

	while(!q.empty())
	{
		auto itr=q.front();
		q.pop();
		ll cost=itr.fi;
		ll x=itr.se.fi;
		ll y=itr.se.se;
		for(ll k=0;k<4;k++)
		{
			ll indx=x+dx[k];
			ll indy=y+dy[k];
			if(indx>=0&&indx<r&&indy>=0&&indy<c&&vec[indx][indy]!='T')
			{
				if(dis[indx][indy]>=cost+1)
				{
					dis[indx][indy]=cost+1;
					q.push(mp(cost+1,mp(indx,indy)));
				}

			}
		}

	}

}

int main()
{
    	fio;
    ll i,j;
    	for(i=0;i<1005;i++)
    	{
    		for(j=0;j<1005;j++)
    		dis[i][j]=INT_MAXI;
		}

    cin>>r>>c;

    for(i=0;i<r;i++)
    {
    	string str;
    	cin>>str;
    	vec.push_back(str);
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]=='S')
			{
				si=i;
				sj=j;
			}
			if(vec[i][j]=='E')
			{
				ei=i;
				ej=j;
			}
		}
	}
	bfs(ei,ej);
	ll ets=dis[si][sj];
	ll ans=0;

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]>='1'&&vec[i][j]<='9')
			{
				if(dis[i][j]<=ets)
				ans+=(vec[i][j]-'0');
			}
		}

	}

   cout<<ans;

}


#include<bits/stdc++.h>
#define ll  int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define piii pair<ll,pair<ll,ll> >
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
//#define INT_MAXI 9000000000000000000

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

 char vec[1005][1005];
ll r,c;
ll dis[1005][1005];

ll	ei,ej,si,sj,count2=0;
void bfs(ll i,ll j)
{
    ll x1=i,x2=j,count;

	queue<pair<ll,pair<ll,ll> > >q;
	q.push(mp(0,mp(i,j)));

	while(!q.empty())
	{
		auto itr=q.front();
		q.pop();
		ll cost=itr.fi;
		ll x=itr.se.fi;
		ll y=itr.se.se;
		for(ll k=0;k<4;k++)
		{
			ll indx=x+dx[k];
			ll indy=y+dy[k];
			if(indx>=0&&indx<r&&indy>=0&&indy<c&&vec[indx][indy]!='T'&&dis[indx][indy]==-1)
			{

					dis[indx][indy]=cost+1;
					q.push(mp(cost+1,mp(indx,indy)));

			}
		}

	}

}

int main()
{
    	fio;
    ll i,j;
    	for(i=0;i<1005;i++)
    	{
    		for(j=0;j<1005;j++)
    		dis[i][j]=-1;
		}

    cin>>r>>c;

    for(i=0;i<r;i++)
    {
    	for(j=0;j<c;j++)
    	cin>>vec[i][j];
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]=='S')
			{
				si=i;
				sj=j;
			}
			if(vec[i][j]=='E')
			{
				ei=i;
				ej=j;
			}
		}
	}
	bfs(ei,ej);
	ll ets=dis[si][sj];
	ll ans=0;
//	cout<<ets;
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			if(vec[i][j]>='1'&&vec[i][j]<='9')
			{
				if(dis[i][j]!=-1&&dis[i][j]<=ets)
				ans+=(vec[i][j]-'0');
			}
		}

	}

   cout<<ans;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define ll long long
#define vec vector<long long >
unsigned long long int dist[1150][1150],visited[1150][1150];
char ans[1050][1050];
void solve()
{

	ll n,i,j,m;
	cin>>n>>m;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		cin>>ans[i][j];
	}
	int startx,starty,endx,endy;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(ans[i][j]=='E')
			{
				endx=i;
				endy=j;
			}
			if(ans[i][j]=='S')
			{
				startx=i;
				starty=j;
			}
		}
	}
	for(i=0;i<n;i++)
	for(j=0;j<m;j++)
	{
		dist[i][j]=INT_MAX;
		visited[i][j]=0;
	}

	int rowcol[]={0,0,1,-1};
	int colcol[]={1,-1,0,0};
	dist[endx][endy]=0;
	visited[endx][endy]=1;
	queue<pair<int,int> > q;
	q.push({endx,endy});
	while(q.empty()==0)
	{
		pair<int,int> point=q.front();
		q.pop();
		int x=point.first;
		int y=point.second;

		for(i=0;i<4;i++)
		{
			int x1=x+rowcol[i];
			int y1=y+colcol[i];

			if(x1>=0&&y1>=0&&x1<n&&y1<m&&ans[x1][y1]!='T')
			{

				if(visited[x1][y1]==0)
				{
				dist[x1][y1]=dist[x][y]+1;
				visited[x1][y1]=1;
				q.push({x1,y1});
				}
			}
		}
	}

	unsigned long long int count=0;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(startx==i&&starty==j)
			{
				continue;
			}
				if(dist[startx][starty]>=dist[i][j]&&ans[i][j]!='0'&&ans[i][j]!='T'&&ans[i][j]!='E')
				count+=ans[i][j]-'0';
		}
	}
	cout<<count<<endl;
}
int main(){
	ll n=1,x1,y1,x2,y2,i;
	//cin>>n;
	while(n--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define ll long long
#define vec vector<long long >
int dist[1050][150];
void solve()
{

	ll n,i,j,m;
	cin>>n>>m;
	string b;
	vector<string> ans;
	for(i=0;i<n;i++)
	{
		cin>>b;
		ans.push_back(b);
	}
	int startx,starty,endx,endy;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(ans[i][j]=='E')
			{
				endx=i;
				endy=j;
			}
			if(ans[i][j]=='S')
			{
				startx=i;
				starty=j;
			}
		}
	}
	for(i=0;i<n;i++)
	for(j=0;j<m;j++)
	dist[i][j]=INT_MAX;

	int rowcol[]={0,0,1,-1};
	int colcol[]={1,-1,0,0};
	dist[endx][endy]=0;
	queue<pair<int,int> > q;
	q.push({endx,endy});
	while(q.empty()==0)
	{
		pair<int,int> point=q.front();
		q.pop();
		int x=point.first;
		int y=point.second;

		for(i=0;i<4;i++)
		{
			int x1=x+rowcol[i];
			int y1=y+colcol[i];

			if(x1>=0&&y1>=0&&x1<n&&y1<m&&ans[x1][y1]!='T')
			{

				if(dist[x1][y1]>dist[x][y]+1)
				{

				dist[x1][y1]=dist[x][y]+1;

				q.push({x1,y1});
				}
			}
		}
	}

	long long int count=0;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(startx==i&&starty==j)
			{
				continue;
			}
				if(dist[startx][starty]>=dist[i][j]&&ans[i][j]!='0'&&ans[i][j]!='T'&&ans[i][j]!='E')
				count+=ans[i][j]-'0';

		}
	}
	cout<<count<<endl;
}
int main(){
	ll n=1,x1,y1,x2,y2,i;
	//cin>>n;
	while(n--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define ll long long
#define vec vector<long long >
long long int dist[1050][150],visited[1050][1050];
char ans[1050][1050];
void solve()
{

	ll n,i,j,m;
	cin>>n>>m;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		cin>>ans[i][j];
	}
	int startx,starty,endx,endy;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(ans[i][j]=='E')
			{
				endx=i;
				endy=j;
			}
			if(ans[i][j]=='S')
			{
				startx=i;
				starty=j;
			}
		}
	}
	for(i=0;i<n;i++)
	for(j=0;j<m;j++)
	{
		dist[i][j]=INT_MAX;
		visited[i][j]=0;
	}

	int rowcol[]={0,0,1,-1};
	int colcol[]={1,-1,0,0};
	dist[endx][endy]=0;
	visited[endx][endy]=1;
	queue<pair<int,int> > q;
	q.push({endx,endy});
	while(q.empty()==0)
	{
		pair<int,int> point=q.front();
		q.pop();
		int x=point.first;
		int y=point.second;

		for(i=0;i<4;i++)
		{
			int x1=x+rowcol[i];
			int y1=y+colcol[i];

			if(x1>=0&&y1>=0&&x1<n&&y1<m&&ans[x1][y1]!='T')
			{

				if(visited[x1][y1]==0)
				{
				dist[x1][y1]=dist[x][y]+1;
				visited[x1][y1]=1;
				q.push({x1,y1});
				}
			}
		}
	}

	long long int count=0;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(startx==i&&starty==j)
			{
				continue;
			}
				if(dist[startx][starty]>=dist[i][j]&&ans[i][j]!='0'&&ans[i][j]!='T'&&ans[i][j]!='E')
				count+=ans[i][j]-'0';
		}
	}
	cout<<count<<endl;
}
int main(){
	ll n=1,x1,y1,x2,y2,i;
	//cin>>n;
	while(n--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define ll long long
#define vec vector<long long >
long long int dist[1050][150];
char ans[1050][1050];
void solve()
{

	ll n,i,j,m;
	cin>>n>>m;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		cin>>ans[i][j];
	}
	int startx,starty,endx,endy;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(ans[i][j]=='E')
			{
				endx=i;
				endy=j;
			}
			if(ans[i][j]=='S')
			{
				startx=i;
				starty=j;
			}
		}
	}
	for(i=0;i<n;i++)
	for(j=0;j<m;j++)
	dist[i][j]=INT_MAX;

	int rowcol[]={0,0,1,-1};
	int colcol[]={1,-1,0,0};
	dist[endx][endy]=0;
	queue<pair<int,int> > q;
	q.push({endx,endy});
	while(q.empty()==0)
	{
		pair<int,int> point=q.front();
		q.pop();
		int x=point.first;
		int y=point.second;

		for(i=0;i<4;i++)
		{
			int x1=x+rowcol[i];
			int y1=y+colcol[i];

			if(x1>=0&&y1>=0&&x1<n&&y1<m&&ans[x1][y1]!='T')
			{

				if(dist[x1][y1]>dist[x][y]+1)
				{

				dist[x1][y1]=dist[x][y]+1;

				q.push({x1,y1});
				}
			}
		}
	}

	long long int count=0;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(startx==i&&starty==j)
			{
				continue;
			}
				if(dist[startx][starty]>=dist[i][j]&&ans[i][j]!='0'&&ans[i][j]!='T'&&ans[i][j]!='E')
				count+=ans[i][j]-'0';
		}
	}
	cout<<count<<endl;
}
int main(){
	ll n=1,x1,y1,x2,y2,i;
	//cin>>n;
	while(n--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define ll long long
#define vec vector<long long >
long long int dist[1050][150];
void solve()
{

	ll n,i,j,m;
	cin>>n>>m;
	string b;
	vector<string> ans;
	for(i=0;i<n;i++)
	{
		cin>>b;
		ans.push_back(b);
	}
	int startx,starty,endx,endy;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(ans[i][j]=='E')
			{
				endx=i;
				endy=j;
			}
			if(ans[i][j]=='S')
			{
				startx=i;
				starty=j;
			}
		}
	}
	for(i=0;i<n;i++)
	for(j=0;j<m;j++)
	dist[i][j]=INT_MAX;

	int rowcol[]={0,0,1,-1};
	int colcol[]={1,-1,0,0};
	dist[endx][endy]=0;
	queue<pair<int,int> > q;
	q.push({endx,endy});
	while(q.empty()==0)
	{
		pair<int,int> point=q.front();
		q.pop();
		int x=point.first;
		int y=point.second;

		for(i=0;i<4;i++)
		{
			int x1=x+rowcol[i];
			int y1=y+colcol[i];

			if(x1>=0&&y1>=0&&x1<n&&y1<m&&ans[x1][y1]!='T')
			{

				if(dist[x1][y1]>dist[x][y]+1)
				{

				dist[x1][y1]=dist[x][y]+1;

				q.push({x1,y1});
				}
			}
		}
	}

	long long int count=0;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(startx==i&&starty==j)
			{
				continue;
			}
				if(dist[startx][starty]>=dist[i][j]&&ans[i][j]!='0'&&ans[i][j]!='T'&&ans[i][j]!='E')
				count+=ans[i][j]-'0';
		}
	}
	cout<<count<<endl;
}
int main(){
	ll n=1,x1,y1,x2,y2,i;
	//cin>>n;
	while(n--)
	{
		solve();
	}
}


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

const int M = 1005, N = 1005, MX = 15;
const int DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};

int m, n, dst[M][N];
bool chk[MX];
char s[M][N];

bool check(int x, int y) {
    if (x == 0 || x == m + 1 || y == 0 || y == n + 1) {
        return false;
    } else {
        return s[x][y] != 'T';
    }
}

void BFS(int x, int y) {
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            dst[i][j] = -1;
        }
    }
    queue<int> qx, qy;
    dst[x][y] = M * N;
    for (qx.push(x), qy.push(y); !qx.empty(); qx.pop(), qy.pop()) {
        int u = qx.front(), v = qy.front();
        for (int i = 0; i < 4; i++) {
            int tx = u + DX[i], ty = v + DY[i];
            if (check(tx, ty) && dst[tx][ty] == M * N) {
                dst[tx][ty] = dst[u][v] + 1;
                qx.push(tx); qy.push(ty);
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        cin >> (s[i] + 1);
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s[i][j] == 'E') {
                BFS(i, j);
            }
        }
    }
    int rep = 0, ans = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s[i][j] == 'S') {
                rep = dst[i][j];
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (isdigit(s[i][j])) {
                ans += (s[i][j] - '0') * (dst[i][j] <= rep);
            }
        }
    }
    cout << ans;
}


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

const int M = 1005, N = 1005, MX = 15;
const int DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};

int m, n, dst[M][N];
bool chk[MX];
char s[M][N];

bool check(int x, int y) {
    if (x == 0 || x == m + 1 || y == 0 || y == n + 1) {
        return false;
    } else {
        return s[x][y] != 'T';
    }
}

void BFS(int x, int y) {
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            dst[i][j] = -1;
        }
    }
    queue<int> qx, qy;
    dst[x][y] = 0;
    for (qx.push(x), qy.push(y); !qx.empty(); qx.pop(), qy.pop()) {
        int u = qx.front(), v = qy.front();
        for (int i = 0; i < 4; i++) {
            int tx = u + DX[i], ty = v + DY[i];
            if (check(tx, ty) && dst[tx][ty] == -1) {
                dst[tx][ty] = dst[u][v] + 1;
                qx.push(tx); qy.push(ty);
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        cin >> (s[i] + 1);
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s[i][j] == 'E') {
                BFS(i, j);
            }
        }
    }
    int rep = 0, ans = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s[i][j] == 'S') {
                rep = dst[i][j];
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (isdigit(s[i][j])) {
                ans += (s[i][j] - '0') * (dst[i][j] <= rep);
            }
        }
    }
    cout << ans;
}


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

const int M = 1005, N = 1005, MX = 15;
const int DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};

int m, n, dst[M][N];
bool chk[MX];
char s[M][N];

bool check(int x, int y) {
    if (x == 0 || x == m + 1 || y == 0 || y == n + 1) {
        return false;
    } else {
        return s[x][y] != 'T';
    }
}

void BFS(int x, int y) {
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            dst[i][j] = M * N;
        }
    }
    queue<int> qx, qy;
    dst[x][y] = 0;
    for (qx.push(x), qy.push(y); !qx.empty(); qx.pop(), qy.pop()) {
        int u = qx.front(), v = qy.front();
        for (int i = 0; i < 4; i++) {
            int tx = u + DX[i], ty = v + DY[i];
            if (check(tx, ty) && dst[tx][ty] == M * N) {
                dst[tx][ty] = dst[u][v] + 1;
                qx.push(tx); qy.push(ty);
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        cin >> (s[i] + 1);
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s[i][j] == 'E') {
                BFS(i, j);
            }
        }
    }
    int rep = 0, ans = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s[i][j] == 'S') {
                rep = dst[i][j];
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (isdigit(s[i][j])) {
                ans += (s[i][j] - '0') * (dst[i][j] <= rep);
            }
        }
    }
    cout << ans;
}


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

const int MXN = 1000 + 5;

int n, m;
char mat[N][N];
queue < pair <int, int> > q;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int d[MXN][MXN], ex, ey, sx, sy, ans;

bool isval(int x, int y)
{
	if (x < 0 || x >= n || y < 0 || y >= m)
		return false;
	if (mat[x][y] == 'T')
		return false;
	return true;
}

int main(){
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if(mat[i][j] == 'E') {
				ex = i;
				ey = j;
			}
			else if(mat[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			d[i][j] = -1;
	d[ex][ey] = 0;
	q.push({ex,ey});
	while(!q.empty()) {
		int ux = q.front().first, uy = q.front().second;
		q.pop();
		for(int i = 0; i < 4; i++)
			if(isval(ux+dx[i], uy+dy[i]) && d[ux+dx[i]][uy+dy[i]] == -1) {
				d[ux+dx[i]][uy+dy[i]] = d[ux][uy]+1;
				q.push({ux+dx[i],uy+dy[i]});
			}
	}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if ((i == ex && j == ey) || (i == sx && j == sy))
				continue;
			if (isval(i,j) && d[i][j] != -1 && d[i][j] <= d[sx][sy]) ans += mat[i][j] - '0';
		}
	cout << ans << endl;
	return 0;
}


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

const int N = 1000 + 5;

int n, m;
char mat[N][N];
queue < pair <int, int> > q;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int d[MXN][MXN], ex, ey, sx, sy, ans;

bool isval(int x, int y)
{
	if (x < 0 || x >= n || y < 0 || y >= m)
		return false;
	if (mat[x][y] == 'T')
		return false;
	return true;
}

int main(){
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if(mat[i][j] == 'E') {
				ex = i;
				ey = j;
			}
			else if(mat[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			d[i][j] = -1;
	d[ex][ey] = 0;
	q.push({ex,ey});
	while(!q.empty()) {
		int ux = q.front().first, uy = q.front().second;
		q.pop();
		for(int i = 0; i < 4; i++)
			if(isval(ux+dx[i], uy+dy[i]) && d[ux+dx[i]][uy+dy[i]] == -1) {
				d[ux+dx[i]][uy+dy[i]] = d[ux][uy]+1;
				q.push({ux+dx[i],uy+dy[i]});
			}
	}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if ((i == ex && j == ey) || (i == sx && j == sy))
				continue;
			if (isval(i,j) && d[i][j] != -1 && d[i][j] <= d[sx][sy]) ans += mat[i][j] - '0';
		}
	cout << ans << endl;
	return 0;
}


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

const int N = 1000 + 5;

int n, m;
char mat[N][N];
queue < pair <int, int> > q;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int d[N][N], ex, ey, sx, sy, ans;

bool isval(int x, int y)
{
	if (x < 0 || x >= n || y < 0 || y >= m)
		return false;
	if (mat[x][y] == 'T')
		return false;
	return true;
}

int main(){
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if(mat[i][j] == 'E') {
				ex = i;
				ey = j;
			}
			else if(mat[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			d[i][j] = -1;
	d[ex][ey] = 0;
	q.push({ex,ey});
	while(!q.empty()) {
		int ux = q.front().first, uy = q.front().second;
		q.pop();
		for(int i = 0; i < 4; i++)
			if(isval(ux+dx[i], uy+dy[i]) && d[ux+dx[i]][uy+dy[i]] == -1) {
				d[ux+dx[i]][uy+dy[i]] = d[ux][uy]+1;
				q.push({ux+dx[i],uy+dy[i]});
			}
	}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if ((i == ex && j == ey) || (i == sx && j == sy))
				continue;
			if (isval(i,j) && d[i][j] != -1 && d[i][j] <= d[sx][sy]) ans += mat[i][j] - '0';
		}
	cout << ans << endl;
	return 0;
}


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

vector<vector<int>> bfs(vector<string> &v, pair<int,int> src){
  vector<vector<int>> dist(v.size(), vector<int>(v[0].size(),0));
  vector<vector<bool>> vis(v.size(), vector<bool>(v[0].size(),0));

  queue <pair<int, int>> q;

  vector<pair<int, int>> d ={{0,1},{0,-1},{1,0},{-1,0}};
  q.push(src);

  vis[src.first][src.second] = 1;

  while(!q.empty()){
  pair<int , int> temp = q.front();
  q.pop();
  vis[temp.first][temp.second]=1;
  for(int i = 0; i<4; i++){

    int  x = temp.first + d[i].first;
    int  y = temp.second + d[i].second;

    if(x < v.size() && y < v[0].size() && v[x][y]!='T' && !vis[x][y])
    { q.push({x, y});
      dist[x][y]=dist[temp.first][temp.second]+1;
      vis[x][y]=1;
    }
   }
}
return dist;
}

int main(){

  int r, c, ans = 0;
  bool e=1, s=1;
  pair<int, int> src, dst;
  vector<string> v;
  vector<vector<int>> dist;
  cin >> r >> c;

  for(int i = 0; i < r ; i++){
    string x;
    cin >> x;

    for(int j=0; j< x.size() && (e||s);j++){
      if(x[j]=='E'){
      dst = {i,j};
      e = 0;
      }
      if(x[j]=='S'){
      src = {i,j};
      s = 0;
      }
     }

    v.push_back(x);
  }

  dist = bfs(v,dst);

  for(int i=0; i<r; i++)
  for(int j=0; j<c; j++){

    if(v[i][j]>'0' && v[i][j]<='9' && (dist[i][j] <= dist[src.first][src.second]) )
    ans+=(v[i][j]-'0');
  }

  cout<<ans;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int r, c;
 int dist[1001][1001];
 bool vis[1001][1001];
 char v[1001][1001];
  void bfs(pair<int,int> src){

  queue <pair<int, int>> q;

  vector<pair<int, int>> d ={{0,1},{0,-1},{1,0},{-1,0}};

  q.push(src);

  dist[src.first][src.second] = 0;
  vis[src.first][src.second] = 1;

  while(!q.empty()){
  pair<int , int> temp = q.front();
  q.pop();
  vis[temp.first][temp.second]=1;
  for(int i = 0; i<4; i++){

    int  x = temp.first + d[i].first;
    int  y = temp.second + d[i].second;

    if(x >= 0 && y >= 0 &&  x < r && y < c && v[x][y]!='T' && !vis[x][y])
    { q.push({x, y});
      dist[x][y]=dist[temp.first][temp.second]+1;
      vis[x][y]=1;
    }
   }
}
return ;
}

int main(){

  int ans = 0;

  pair<int, int> src, dst;

  cin >> r >> c;

  for(int i = 0; i < r ; i++)
  for(int j = 0; j < c ; j++){

    dist[i][j] = INT_MAX;

      cin >> v[i][j];

      if(v[i][j]=='E')
      dst = {i,j};

      if(v[i][j]=='S')
      src = {i,j};

     }

   bfs(dst);

  for(int i=0; i<r; i++)
  for(int j=0; j<c; j++){

    if(v[i][j]>'0' && v[i][j]<='9' && (dist[i][j] <= dist[src.first][src.second]) )
    ans+=(v[i][j]-'0');
  }

  cout<<ans;
  return 0;
}


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

int main(){
  double a,b,c,d;
  cin>>a>>b>>c>>d;

  double ans = (a/b)/(1 - (1-c/d)*(1-a/b) ) ;
  cout<<ans<<"\n";
  return 0;
}


/*

ATTENTION: x>=0 && y>=0 condition not added in submission 69464419
THIS IS THE CORRECT SOLUTION USING VECTORS!!!
*/

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

vector<vector<int>> bfs(vector<string> &v, pair<int,int> src){
  vector<vector<int>> dist(v.size(), vector<int>(v[0].size(),INT_MAX));
  vector<vector<bool>> vis(v.size(), vector<bool>(v[0].size(),0));

  queue <pair<int, int>> q;

  vector<pair<int, int>> d ={{0,1},{0,-1},{1,0},{-1,0}};
  q.push(src);
  dist[src.first][src.second] = 0;
  vis[src.first][src.second] = 1;

  while(!q.empty()){
  pair<int , int> temp = q.front();
  q.pop();
  vis[temp.first][temp.second]=1;
  for(int i = 0; i<4; i++){

    int  x = temp.first + d[i].first;
    int  y = temp.second + d[i].second;

    if(x>=0 && y>=0 && x < v.size() && y < v[0].size() && v[x][y]!='T' && !vis[x][y])
    { q.push({x, y});
      dist[x][y]=dist[temp.first][temp.second]+1;
      vis[x][y]=1;
    }
   }
}
return dist;
}

int main(){

  int r, c, ans = 0;
  bool e=1, s=1;
  pair<int, int> src, dst;
  vector<string> v;
  vector<vector<int>> dist;
  cin >> r >> c;

  for(int i = 0; i < r ; i++){
    string x;
    cin >> x;

    for(int j=0; j< x.size() && (e||s);j++){
      if(x[j]=='E'){
      dst = {i,j};
      e = 0;
      }
      if(x[j]=='S'){
      src = {i,j};
      s = 0;
      }
     }

    v.push_back(x);
  }

  dist = bfs(v,dst);

  for(int i=0; i<r; i++)
  for(int j=0; j<c; j++){

    if(v[i][j]>'0' && v[i][j]<='9' && (dist[i][j] <= dist[src.first][src.second]) )
    ans+=(v[i][j]-'0');
  }

  cout<<ans;
  return 0;
}


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

vector<vector<int>> bfs(vector<string> &v, pair<int,int> src){
  vector<vector<int>> dist(v.size(), vector<int>(v[0].size(),INT_MAX));
  vector<vector<bool>> vis(v.size(), vector<bool>(v[0].size(),0));

  queue <pair<int, int>> q;

  vector<pair<int, int>> d ={{0,1},{0,-1},{1,0},{-1,0}};
  q.push(src);
  dist[src.first][src.second] = 0;
  vis[src.first][src.second] = 1;

  while(!q.empty()){
  pair<int , int> temp = q.front();
  q.pop();
  vis[temp.first][temp.second]=1;
  for(int i = 0; i<4; i++){

    int  x = temp.first + d[i].first;
    int  y = temp.second + d[i].second;

    if(x < v.size() && y < v[0].size() && v[x][y]!='T' && !vis[x][y])
    { q.push({x, y});
      dist[x][y]=dist[temp.first][temp.second]+1;
      vis[x][y]=1;
    }
   }
}
return dist;
}

int main(){

  int r, c, ans = 0;
  bool e=1, s=1;
  pair<int, int> src, dst;
  vector<string> v;
  vector<vector<int>> dist;
  cin >> r >> c;

  for(int i = 0; i < r ; i++){
    string x;
    cin >> x;

    for(int j=0; j< x.size() && (e||s);j++){
      if(x[j]=='E'){
      dst = {i,j};
      e = 0;
      }
      if(x[j]=='S'){
      src = {i,j};
      s = 0;
      }
     }

    v.push_back(x);
  }

  dist = bfs(v,dst);

  for(int i=0; i<r; i++)
  for(int j=0; j<c; j++){

    if(v[i][j]>'0' && v[i][j]<='9' && (dist[i][j] <= dist[src.first][src.second]) )
    ans+=(v[i][j]-'0');
  }

  cout<<ans;
  return 0;
}


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

int main(){
  long long n,m,mini,maxi;

  cin>>n>>m;

  if(n==m)
  mini= 0;

  else if(m==1)
  mini = ((n-m+1)*(n-m))/2;

  else{
    long long x = n/m;
    mini = (m-(n%m))*((x*(x-1))/2) + ((x*(x+1))/2)*n%m;
  }

  maxi = ((n-m+1)*(n-m))/2;

  cout<<mini<<" "<<maxi<<"\n";
  return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

int INF = -1;

void djikstra(char **s,int startX,int startY,int r,int c,int **cost) {
    priority_queue<pair<int,pair<int,int>>> pr;
    pr.push(make_pair(0,make_pair(startX,startY)));
    cost[startX][startY] = 0;

    while(!pr.empty()) {
        int tempX = pr.top().second.first,tempY = pr.top().second.second;
        pr.pop();

        if((tempX > 0) && (s[tempX-1][tempY] != 'T')) {
            if((cost[tempX-1][tempY] == INF) || (cost[tempX-1][tempY] > cost[tempX][tempY]+1)) {
                cost[tempX-1][tempY] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX-1][tempY],make_pair(tempX-1,tempY)));
            }
        }
        if((tempY > 0) && (s[tempX][tempY-1] != 'T')) {
            if((cost[tempX][tempY-1] == INF) || (cost[tempX][tempY-1] > cost[tempX][tempY]+1)) {
                cost[tempX][tempY-1] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX][tempY-1],make_pair(tempX,tempY-1)));
            }
        }
        if((tempX < r-1) && (s[tempX+1][tempY] != 'T')) {
            if((cost[tempX+1][tempY] == INF) || (cost[tempX+1][tempY] > cost[tempX][tempY]+1)) {
                cost[tempX+1][tempY] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX+1][tempY],make_pair(tempX+1,tempY)));
            }
        }
        if((tempY < c-1) && (s[tempX][tempY+1] != 'T')) {
            if((cost[tempX][tempY+1] == INF) || (cost[tempX][tempY+1] > cost[tempX][tempY]+1)) {
                cost[tempX][tempY+1] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX][tempY+1],make_pair(tempX,tempY+1)));
            }
        }
    }
}

int main() {
    int row,col,startX = 0,startY = 0,endX = 0,endY = 0;
    cin>>row>>col;
    int **cost;
    char **s;
    cost = new int*[row];
    s = new char*[row];
    stack<pair<int,int>> st;
    for(int i=0;i<row;i++) {
        s[i] = new char[col+1];
        cost[i] = new int[col];
        for(int j=0;j<col;j++) {
            cost[i][j] = INF;
            cin>>s[i][j];
            if(s[i][j] == 'S') {
                startX = i;
                startY = j;
            }
            else if(s[i][j] == 'E') {
                endX = i;
                endY = j;
            }
            else if(s[i][j] != '0' && s[i][j] != 'T') {
                st.push(make_pair(i,j));
            }
        }
    }
    cost[endX][endY] = 0;

    int count = 0;
    djikstra(s,endX,endY,row,col,cost);

    while(!st.empty()) {
        int x = st.top().first;
        int y = st.top().second;
        if(cost[x][y] <= cost[startX][startY]) {
            count += (int)(s[x][y]-'0');
        }
        st.pop();
    }

    cout<<count<<'\n';
    return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

int INF = -1;

void djikstra(char **s,int startX,int startY,int r,int c,int **cost) {
    priority_queue<pair<int,pair<int,int>>> pr;
    pr.push(make_pair(0,make_pair(startX,startY)));
    cost[startX][startY] = 0;

    while(!pr.empty()) {
        int tempX = pr.top().second.first,tempY = pr.top().second.second;
        pr.pop();

        if((tempX > 0) && (s[tempX-1][tempY] != 'T')) {
            if((cost[tempX-1][tempY] == INF) || (cost[tempX-1][tempY] > cost[tempX][tempY]+1)) {
                cost[tempX-1][tempY] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX-1][tempY],make_pair(tempX-1,tempY)));
            }
        }
        if((tempY > 0) && (s[tempX][tempY-1] != 'T')) {
            if((cost[tempX][tempY-1] == INF) || (cost[tempX][tempY-1] > cost[tempX][tempY]+1)) {
                cost[tempX][tempY-1] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX][tempY-1],make_pair(tempX,tempY-1)));
            }
        }
        if((tempX < r-1) && (s[tempX+1][tempY] != 'T')) {
            if((cost[tempX+1][tempY] == INF) || (cost[tempX+1][tempY] > cost[tempX][tempY]+1)) {
                cost[tempX+1][tempY] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX+1][tempY],make_pair(tempX+1,tempY)));
            }
        }
        if((tempY < c-1) && (s[tempX][tempY+1] != 'T')) {
            if((cost[tempX][tempY+1] == INF) || (cost[tempX][tempY+1] > cost[tempX][tempY]+1)) {
                cost[tempX][tempY+1] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX][tempY+1],make_pair(tempX,tempY+1)));
            }
        }
    }
}

int main() {
    int row,col,startX = 0,startY = 0,endX = 0,endY = 0;
    cin>>row>>col;
    int **cost;
    char **s;
    cost = new int*[row];
    s = new char*[row];
    stack<pair<int,int>> st;
    for(int i=0;i<row;i++) {
        s[i] = new char[col+1];
        cost[i] = new int[col];
        for(int j=0;j<col;j++) {
            cost[i][j] = INF;
            cin>>s[i][j];
            if(s[i][j] == 'S') {
                startX = i;
                startY = j;
            }
            else if(s[i][j] == 'E') {
                endX = i;
                endY = j;
            }
            else if(s[i][j] != '0' && s[i][j] != 'T') {
                st.push(make_pair(i,j));
            }
        }
    }
    cost[endX][endY] = 0;

    int count = 0;
    djikstra(s,endX,endY,row,col,cost);

    while(!st.empty()) {
        int x = st.top().first;
        int y = st.top().second;
        if(cost[x][y] <= cost[startX][startY] && cost[x][y] != INF) {
            count += (int)(s[x][y]-'0');
        }
        st.pop();
    }

    cout<<count<<'\n';
    return 0;
}


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

int INF = -1;

int djikstra(char **s,int startX,int startY,int endX,int endY,int r,int c) {
    int cost[r][c];
    for(int i=0;i<r;i++) {
        for(int j=0;j<c;j++) {
            cost[i][j] = INF;
        }
    }

    priority_queue<pair<int,pair<int,int> > > pr;
    pr.push(make_pair(0,make_pair(startX,startY)));
    cost[startX][startY] = 0;

    while(!pr.empty()) {
        int tempX = pr.top().second.first,tempY = pr.top().second.second;
        pr.pop();
        if(tempX == endX && tempY == endY) {
            return cost[endX][endY];
        }

        if((tempX > 0) && (s[tempX-1][tempY] != 'T')) {
            if((cost[tempX-1][tempY] == INF) || (cost[tempX-1][tempY] > cost[tempX][tempY]+1)) {
                cost[tempX-1][tempY] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX-1][tempY],make_pair(tempX-1,tempY)));
            }
        }
        if((tempY > 0) && (s[tempX][tempY-1] != 'T')) {
            if((cost[tempX][tempY-1] == INF) || (cost[tempX][tempY-1] > cost[tempX][tempY]+1)) {
                cost[tempX][tempY-1] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX][tempY-1],make_pair(tempX,tempY-1)));
            }
        }
        if((tempX < r-1) && (s[tempX+1][tempY] != 'T')) {
            if((cost[tempX+1][tempY] == INF) || (cost[tempX+1][tempY] > cost[tempX][tempY]+1)) {
                cost[tempX+1][tempY] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX+1][tempY],make_pair(tempX+1,tempY)));
            }
        }
        if((tempY < c-1) && (s[tempX][tempY+1] != 'T')) {
            if((cost[tempX][tempY+1] == INF) || (cost[tempX][tempY+1] > cost[tempX][tempY]+1)) {
                cost[tempX][tempY+1] = cost[tempX][tempY]+1;
                pr.push(make_pair(-cost[tempX][tempY+1],make_pair(tempX,tempY+1)));
            }
        }
    }

    return INF;
}

int main() {
    int row,col,startX = 0,startY = 0,endX = 0,endY = 0;
    cin>>row>>col;
    char **s;
    s = new char*[row];
    stack<pair<int,int>> st;
    for(int i=0;i<row;i++) {
        s[i] = new char[col+1];
        for(int j=0;j<col;j++) {
            cin>>s[i][j];
            if(s[i][j] == 'S') {
                startX = i;
                startY = j;
            }
            else if(s[i][j] == 'E') {
                endX = i;
                endY = j;
            }
            else if(s[i][j] != '0' && s[i][j] != 'T') {
                st.push(make_pair(i,j));
            }
        }
    }

    int count = 0;
    int pathDis = djikstra(s,startX,startY,endX,endY,row,col);

    while(!st.empty()) {
        int temp = djikstra(s,st.top().first,st.top().second,endX,endY,row,col);
        count = ((temp != INF) && (temp <= pathDis)) ? count+(int)(s[st.top().first][st.top().second]-'0') : count;
        st.pop();
    }

    cout<<count<<'\n';
    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;
	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 (d1 <= d2 && s[i][j] != 'E' && s[i][j] != 'S' && s[i][j] != 'T')
			{
				ans += (s[i][j] - '0');
			}
		}
	}
	cout << ans << en;

	return 0;
}