#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
char s[1005][1005];
bool v[1005][1005];
struct ajab {
  int x;
  int y;
  int q;
  ajab(int b, int m, int n) {
    x = b;
    y = m;
    q = n;
  }
};
queue<ajab> Q;
int main() {
  int q, e;
  int r, c;
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      a[i][j] = -1;
    }
  }
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      cin >> s[i][j];
      if (s[i][j] == 'E') {
        q = i;
        e = j;
      }
    }
  int tt = 1000;
  v[q][e] = true;
  ajab ww(q, e, 0);
  Q.push(ww);
  int l = 0;
  while (!Q.empty()) {
    ajab p = Q.front();
    if (p.x != 0 && s[p.x - 1][p.y] != 'T' && !v[p.x - 1][p.y]) {
      Q.push(ajab(p.x - 1, p.y, p.q + 1));
      v[p.x - 1][p.y] = true;
    }
    if (p.x != r - 1 && s[p.x + 1][p.y] != 'T' && !v[p.x + 1][p.y]) {
      Q.push(ajab(p.x + 1, p.y, p.q + 1));
      v[p.x + 1][p.y] = true;
    }
    if (p.y != 0 && s[p.x][p.y - 1] != 'T' && !v[p.x][p.y - 1]) {
      Q.push(ajab(p.x, p.y - 1, p.q + 1));
      v[p.x][p.y - 1] = true;
    }
    if (p.y != c - 1 && s[p.x][p.y + 1] != 'T' && !v[p.x][p.y + 1]) {
      Q.push(ajab(p.x, p.y + 1, p.q + 1));
      v[p.x][p.y + 1] = true;
    }
    if (s[p.x][p.y] == 'S') {
      tt = p.q;
    }
    a[p.x][p.y] = p.q;
    Q.pop();
  }
  int qqq = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (s[i][j] != 'T' && s[i][j] != '0' && s[i][j] != 'S' &&
          s[i][j] != 'E' && a[i][j] <= tt && a[i][j] != -1) {
        qqq += s[i][j] - '0';
      }
    }
  }
  cout << qqq << endl;
}
