#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, z;
};
int i, j, m, n, x2, y2, ct, ans;
queue<node> q;
node k, l;
bool booleen;
char c[1001];
char a[1001][1001];
bool b[1001][1001];
int main() {
  scanf("%d%d\n", &m, &n);
  for (i = 1; i <= m; i++) {
    gets(c);
    for (j = 1; j <= n; j++) {
      a[i][j] = c[j - 1];
      if (a[i][j] == 'S') {
        k.x = i;
        k.y = j;
        x2 = i;
        y2 = j;
        k.z = 0;
      }
    }
  }
  q.push(k);
  while (!q.empty()) {
    l = q.front();
    if (a[l.x][l.y] == 'E') {
      ct = l.z;
      break;
    }
    q.pop();
    i = l.x;
    j = l.y;
    if (i > 1 && !b[i - 1][j] && a[i - 1][j] != 'T') {
      b[i - 1][j] = 1;
      k.x = i - 1;
      k.y = j;
      k.z = l.z + 1;
      q.push(k);
    }
    if (i < m && !b[i + 1][j] && a[i + 1][j] != 'T') {
      b[i + 1][j] = 1;
      k.x = i + 1;
      k.y = j;
      k.z = l.z + 1;
      q.push(k);
    }
    if (j > 1 && !b[i][j - 1] && a[i][j - 1] != 'T') {
      b[i][j - 1] = 1;
      k.x = i;
      k.y = j - 1;
      k.z = l.z + 1;
      q.push(k);
    }
    if (j < n && !b[i][j + 1] && a[i][j + 1] != 'T') {
      b[i][j + 1] = 1;
      k.x = i;
      k.y = j + 1;
      k.z = l.z + 1;
      q.push(k);
    }
  }
  memset(b, 0, sizeof(b));
  j = q.size();
  for (i = 1; i <= j; i++) q.pop();
  k.x = l.x;
  k.y = l.y;
  k.z = 0;
  q.push(k);
  while (!q.empty()) {
    l = q.front();
    i = l.x;
    j = l.y;
    if (l.z > ct) break;
    if (a[l.x][l.y] >= 48 && a[l.x][l.y] <= 57) ans += a[l.x][l.y] - 48;
    q.pop();
    if (i > 1 && !b[i - 1][j] && a[i - 1][j] != 'T') {
      b[i - 1][j] = 1;
      k.x = i - 1;
      k.y = j;
      k.z = l.z + 1;
      q.push(k);
    }
    if (i < m && !b[i + 1][j] && a[i + 1][j] != 'T') {
      b[i + 1][j] = 1;
      k.x = i + 1;
      k.y = j;
      k.z = l.z + 1;
      q.push(k);
    }
    if (j > 1 && !b[i][j - 1] && a[i][j - 1] != 'T') {
      b[i][j - 1] = 1;
      k.x = i;
      k.y = j - 1;
      k.z = l.z + 1;
      q.push(k);
    }
    if (j < n && !b[i][j + 1] && a[i][j + 1] != 'T') {
      b[i][j + 1] = 1;
      k.x = i;
      k.y = j + 1;
      k.z = l.z + 1;
      q.push(k);
    }
  }
  printf("%d\n", ans);
  return 0;
}
