#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:300000000")
const long double eps = 1e-20;
const long double pi = acos(-1.0);
const long long inf = 1000 * 1000 * 1000 * 1000 * 1000 * 1000;
const long long base = 1000 * 1000 * 1000 + 7;
using namespace std;
int n, m, xs, ys, un, uk, xx, yy, ans;
string s[1111];
pair<int, int> q[1000 * 1000 + 20];
int t[1111][1111];
void add(int x, int y, int xx, int yy) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] == 'T' || t[x][y] != 0)
    return;
  t[x][y] = t[xx][yy] + 1;
  q[++uk] = make_pair(x, y);
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < (n); i++) cin >> s[i];
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++)
      if (s[i][j] == 'E') {
        xs = i;
        ys = j;
      }
  t[xs][ys] = 1;
  q[++uk] = make_pair(xs, ys);
  while (un ^ uk) {
    un++;
    xx = q[un].first;
    yy = q[un].second;
    add(xx - 1, yy, xx, yy);
    add(xx + 1, yy, xx, yy);
    add(xx, yy - 1, xx, yy);
    add(xx, yy + 1, xx, yy);
  }
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++)
      if (s[i][j] == 'S') {
        xs = i;
        ys = j;
      }
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++)
      if ('0' <= s[i][j] && s[i][j] <= '9' && t[i][j] <= t[xs][ys] &&
          t[i][j] != 0)
        ans += (int)(s[i][j] - '0');
  cout << ans;
  return 0;
}
