#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 10;
int r, c;
pair<int, int> e, s;
char a[MAXN][MAXN];
int lev[MAXN][MAXN], ans;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void bfs() {
  queue<pair<int, int>> q;
  q.push(e);
  lev[e.first][e.second] = 1;
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++)
      if (lev[u.first + dx[i]][u.second + dy[i]] >
              lev[u.first][u.second] + 1 and
          a[u.first + dx[i]][u.second + dy[i]] != 'T') {
        lev[u.first + dx[i]][u.second + dy[i]] = lev[u.first][u.second] + 1;
        q.push({u.first + dx[i], u.second + dy[i]});
      }
  }
}
int main() {
  memset(lev, 5, sizeof(lev));
  memset(a, 'T', sizeof(a));
  cin >> r >> c;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'E') e = {i, j};
      if (a[i][j] == 'S') s = {i, j};
    }
  bfs();
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++)
      if (lev[i][j] <= lev[s.first][s.second] and isdigit(a[i][j]))
        ans += int(a[i][j] - '0');
  cout << ans << endl;
  return 0;
}
