#include <bits/stdc++.h>
using namespace std;
inline long long in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline string get() {
  char ch[2000010];
  scanf("%s", ch);
  return ch;
}
inline void read(long long *a, long long n) {
  for (long long i = 0; i < n; i++) a[i] = in();
}
template <class blank>
inline void out(blank x) {
  cout << x << "\n";
  exit(0);
}
const long long maxn = 1e5 + 10;
const long long maxm = 1e6 + 10;
const long long maxlg = 21;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 1e18 + 10;
const long long SQ = 317 + 5;
pair<long long, long long> source, sink;
long long enemy[1005][1005];
bool tree[1005][1005];
long long di[1005][1005];
long long dx[] = {0, 0, -1, 1};
long long dy[] = {1, -1, 0, 0};
pair<long long, long long> q[1005 * 1005];
long long n, m;
inline void bfs() {
  long long h = 0, t = 1;
  q[0] = {source.first, source.second};
  memset(di, 63, sizeof di);
  di[source.first][source.second] = 0;
  while (h < t) {
    long long x = q[h].first, y = q[h].second;
    h++;
    for (long long i = 0; i < 4; i++) {
      long long x2 = x + dx[i], y2 = y + dy[i];
      if (x2 >= 0 && y2 >= 0 && x2 < n && y2 < m && !tree[x2][y2] &&
          di[x2][y2] > di[x][y] + 1) {
        di[x2][y2] = di[x][y] + 1;
        q[t++] = {x2, y2};
      }
    }
  }
}
long long res;
int32_t main() {
  n = in(), m = in();
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      char ch = 0;
      do {
        ch = getchar();
      } while (!isalpha(ch) && !(ch - '0' >= 0 && ch - '0' <= 9));
      if (ch == 'S')
        source = {i, j};
      else if (ch == 'E')
        sink = {i, j};
      else if (!isalpha(ch))
        enemy[i][j] = ch - '0';
      else
        tree[i][j] = true;
    }
  swap(source, sink);
  bfs();
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++)
      if (enemy[i][j] && di[i][j] <= di[sink.first][sink.second])
        res += enemy[i][j];
  cout << res << "\n";
}
