#include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b, long long mod) {
  if (b == 0 || a == 1) {
    if (mod == 1)
      return 0;
    else
      return 1;
  }
  if (b % 2 == 0) {
    long long k = powmod(a, b / 2, mod);
    return (k * k) % mod;
  } else {
    long long k = powmod(a, b / 2, mod);
    return ((k * k) % mod * a) % mod;
  }
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a > b)
    return gcd(a % b, b);
  else
    return gcd(b % a, a);
}
long long prime(long long p) {
  for (long long i = 2; i * i <= p; i++) {
    if (p % i == 0 && i < p) return i;
  }
  return 1;
}
long long sqr(long long i) { return i * i; }
void solve(long long ppppppppp = 1) {
  long long a, b;
  cin >> a >> b;
  char m[a][b];
  for (long long i = 0; i < a; i++) {
    for (long long j = 0; j < b; j++) cin >> m[i][j];
  }
  long long x1, y1;
  long long x2, y2;
  vector<pair<long long, pair<long long, long long>>> p;
  for (long long i = 0; i < a; i++) {
    for (long long j = 0; j < b; j++) {
      if (m[i][j] == 'S') {
        x1 = i;
        y1 = j;
      }
      if (m[i][j] == 'E') {
        x2 = i;
        y2 = j;
      }
      if (m[i][j] >= '0' && m[i][j] <= '9') {
        p.push_back({m[i][j] - '0', {i, j}});
      }
    }
  }
  queue<pair<long long, long long>> q;
  bool used[a][b];
  for (long long i = 0; i < a; i++) {
    for (long long j = 0; j < b; j++) used[i][j] = false;
  }
  q.push({x2, y2});
  used[x2][y2] = true;
  long long cur[a][b];
  cur[x2][y2] = 0;
  long long y = 0;
  while (!q.empty() && y <= 10) {
    pair<long long, long long> c = q.front();
    q.pop();
    if (c.first >= 1 && used[c.first - 1][c.second] == false &&
        m[c.first - 1][c.second] != 'T') {
      q.push({c.first - 1, c.second});
      used[c.first - 1][c.second] = true;
      cur[c.first - 1][c.second] = cur[c.first][c.second] + 1;
    }
    if (c.first < a - 1 && used[c.first + 1][c.second] == false &&
        m[c.first + 1][c.second] != 'T') {
      q.push({c.first + 1, c.second});
      used[c.first + 1][c.second] = true;
      cur[c.first + 1][c.second] = cur[c.first][c.second] + 1;
    }
    if (c.second >= 1 && used[c.first][c.second - 1] == false &&
        m[c.first][c.second - 1] != 'T') {
      q.push({c.first, c.second - 1});
      used[c.first][c.second - 1] = true;
      cur[c.first][c.second - 1] = cur[c.first][c.second] + 1;
    }
    if (c.second < b - 1 && used[c.first][c.second + 1] == false &&
        m[c.first][c.second + 1] != 'T') {
      q.push({c.first, c.second + 1});
      used[c.first][c.second + 1] = true;
      cur[c.first][c.second + 1] = cur[c.first][c.second] + 1;
    }
  }
  long long ans = 0;
  for (long long i = 0; i < p.size(); i++)
    if (used[p[i].second.first][p[i].second.second] &&
        cur[p[i].second.first][p[i].second.second] <= cur[x1][y1]) {
      ans += m[p[i].second.first][p[i].second.second] - '0';
    }
  cout << ans;
  return;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tututu;
  tututu = 1;
  for (long long qwerty = 0; qwerty < tututu; qwerty++) solve();
  return 0;
}
