#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int MaxN = int(2e5) + 256;
const int MOD = int(1e9) + 7;
template <typename T>
inline T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
inline bool Palindrome(const string& s) {
  return equal(s.begin(), s.end(), s.rbegin());
}
char s[1500][1500];
int d[1500][1500];
int sx, sy, ex, ey;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("\n%s", s[i]);
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') {
        sx = i, sy = j;
      }
      if (s[i][j] == 'E') {
        ex = i, ey = j;
      }
    }
  }
  queue<pair<int, int> > q;
  q.push(make_pair(ex, ey));
  memset(d, 255, sizeof d);
  d[ex][ey] = 0;
  while (!q.empty()) {
    auto x = q.front().first, y = q.front().second;
    q.pop();
    for (int dx = -1; dx <= 1; ++dx) {
      for (int dy = -1; dy <= 1; ++dy)
        if (abs(dx) + abs(dy) == 1) {
          int nx = x + dx, ny = y + dy;
          if (0 <= nx && 0 <= ny && nx < n && ny < m && s[nx][ny] != 'T' &&
              d[nx][ny] == -1) {
            d[nx][ny] = d[x][y] + 1;
            q.push(make_pair(nx, ny));
          }
        }
    }
  }
  int res = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (isdigit(s[i][j]) && d[i][j] <= d[sx][sy] && d[i][j] != -1)
        res += s[i][j] - '0';
  printf("%d\n", res);
  return 0;
}
