#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
string s[1005];
int n, m, ans, d[1005][1005];
pair<int, int> now, goal;
queue<pair<int, int> > q;
bool check(pair<int, int> a) {
  if (a.first < 0 || a.first >= n || a.second < 0 || a.second >= m ||
      s[a.first][a.second] == 'T')
    return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      d[i][j] = 0x3f3f3f3f;
      if (s[i][j] == 'E') now.first = i, now.second = j, d[i][j] = 0;
      if (s[i][j] == 'S') goal.first = i, goal.second = j;
    }
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      pair<int, int> next = now;
      next.first += dx[i];
      next.second += dy[i];
      if (check(next)) continue;
      if (d[next.first][next.second] > d[now.first][now.second] + 1) {
        d[next.first][next.second] = d[now.first][now.second] + 1;
        q.push(next);
      }
    }
  }
  int pp = d[goal.first][goal.second];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (s[i][j] <= '9' && s[i][j] >= '0' && d[i][j] - 1 < pp)
        ans += s[i][j] - '0';
  printf("%d", ans);
  return 0;
}
