#include <bits/stdc++.h>
using namespace std;
inline int tavan(int a, int n, int mod) {
  int p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
int n, m, d[(1111)][(1111)];
string s[(1111)];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> 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++)
      if (s[i][j] == 'E') {
        queue<pair<int, int> > q;
        d[i][j] = 1;
        q.push({i, j});
        while (q.size()) {
          int a = q.front().first, b = q.front().second;
          q.pop();
          if (a > 0 && !d[a - 1][b] && s[a - 1][b] != 'T')
            d[a - 1][b] = d[a][b] + 1, q.push({a - 1, b});
          if (a < n - 1 && !d[a + 1][b] && s[a + 1][b] != 'T')
            d[a + 1][b] = d[a][b] + 1, q.push({a + 1, b});
          if (b > 0 && !d[a][b - 1] && s[a][b - 1] != 'T')
            d[a][b - 1] = d[a][b] + 1, q.push({a, b - 1});
          if (b < m - 1 && !d[a][b + 1] && s[a][b + 1] != 'T')
            d[a][b + 1] = d[a][b] + 1, q.push({a, b + 1});
        }
        int now, ans = 0;
        for (int k1 = 0; k1 < n; k1++)
          for (int k2 = 0; k2 < m; k2++)
            if (s[k1][k2] == 'S') {
              now = d[k1][k2];
              break;
            }
        for (int k1 = 0; k1 < n; k1++)
          for (int k2 = 0; k2 < m; k2++)
            if ('0' < s[k1][k2] && s[k1][k2] <= '9' && d[k1][k2] <= now &&
                d[k1][k2])
              ans += s[k1][k2] - '0';
        cout << ans;
        break;
      }
  return 0;
}
