#include <bits/stdc++.h>
using namespace std;
int on_bit(int x, int pos) {
  x |= (1 << pos);
  return x;
}
int off_bit(int x, int pos) {
  x &= ~(1 << pos);
  return x;
}
bool is_on_bit(int x, int pos) { return ((x & (1 << pos)) != 0); }
int flip_bit(int x, int pos) {
  x ^= (1 << pos);
  return x;
}
int lsb(int x) { return x & (-x); }
int on_bit_all(int x, int pos) {
  x = (1 << pos) - 1;
  return x;
}
const double EPS = 1e-9;
const double PI = 2 * acos(0.0);
const int INF = 0x3f3f3f3f;
const long long LLINF = 1e18;
const int MOD = 1e9 + 7;
int add(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; }
int sub(long long a, long long b) { return ((a % MOD) - (b % MOD)) % MOD; }
int mult(long long a, long long b) { return ((a % MOD) * (b % MOD)) % MOD; }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 1005;
int h, w;
char a[N][N];
int dist[N][N];
bool vis[N][N];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool v(int y, int x) { return y >= 0 && x >= 0 && y < h && x < w; }
void bfs(int y, int x) {
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair(pair<int, int>(y, x), 0));
  vis[y][x] = true;
  while (!q.empty()) {
    pair<int, int> u = q.front().first;
    int cost = q.front().second;
    int uy = u.first, ux = u.second;
    q.pop();
    dist[uy][ux] = cost;
    for (int i = 0; i < (int)4; i++) {
      int nxy = uy + dy[i];
      int nxx = ux + dx[i];
      if (v(nxy, nxx) && !vis[nxy][nxx] && a[nxy][nxx] != 'T') {
        q.push(make_pair(pair<int, int>(nxy, nxx), cost + 1));
        vis[nxy][nxx] = true;
      }
    }
  }
}
int main() {
  for (int i = 0; i < (int)N - 2; i++)
    for (int j = 0; j < (int)N - 2; j++) dist[i][j] = INF;
  memset(vis, 0, sizeof(vis));
  scanf("%d%d", &h, &w);
  for (int i = 0; i < (int)h; i++) scanf("%s", a[i]);
  for (int i = 0; i < (int)h; i++) {
    for (int j = 0; j < (int)w; j++) {
      if (a[i][j] == 'E') bfs(i, j);
    }
  }
  int sp;
  for (int i = 0; i < (int)h; i++) {
    for (int j = 0; j < (int)w; j++) {
      if (a[i][j] == 'S') sp = dist[i][j];
    }
  }
  int res = 0;
  for (int i = 0; i < (int)h; i++) {
    for (int j = 0; j < (int)w; j++) {
      if (a[i][j] >= '1' && a[i][j] <= '9') {
        if (dist[i][j] <= sp) res += a[i][j] - '0';
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
