#include <bits/stdc++.h>
using namespace std;
long long mod = 100000000;
long long power(long long n, long long k) {
  long long ans = 1;
  while (k) {
    if (k & 1) ans = (ans * n) % mod;
    k /= 2;
    n = (n * n) % mod;
  }
  return ans;
}
int n, m;
bool issafe(int i, int j) {
  if (i < 0 || i >= n || j < 0 || j >= m)
    return 0;
  else
    return 1;
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  string s[n];
  for (int i = 0; i < int(n); i++) cin >> s[i];
  int dp[n][m];
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) dp[i][j] = INT_MAX;
  queue<pair<int, int> > que;
  int r, c;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++)
      if (s[i][j] == 'E') {
        r = i;
        c = j;
        break;
      }
  int x, y;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++)
      if (s[i][j] == 'S') {
        x = i;
        y = j;
        break;
      }
  que.push({r, c});
  dp[r][c] = 0;
  int vis[n][m];
  memset(vis, 0, sizeof(vis));
  vis[r][c] = 1;
  while (!que.empty()) {
    r = que.front().first;
    c = que.front().second;
    que.pop();
    for (int i = 0; i < int(4); i++) {
      if (issafe(r + dx[i], c + dy[i])) {
        if (vis[r + dx[i]][c + dy[i]] == 0 && s[r + dx[i]][c + dy[i]] != 'T') {
          vis[r + dx[i]][c + dy[i]] = 1;
          dp[r + dx[i]][c + dy[i]] = dp[r][c] + 1;
          que.push({r + dx[i], c + dy[i]});
        }
      }
    }
  }
  int ans = 0, che = dp[x][y];
  for (int i = 0; i < int(n); i++) {
    for (int j = 0; j < int(m); j++) {
      if (s[i][j] != 'T' && s[i][j] != 'S' && s[i][j] != 'E') {
        int abc = s[i][j] - '0';
        if (dp[i][j] <= che) ans += abc;
      }
    }
  }
  cout << ans;
  return 0;
}
