#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
long long n, m;
char A[1002][1002];
int lene[1002][1002];
int ox[] = {-1, 0, 1, 0};
int oy[] = {0, 1, 0, -1};
bool viz[1002][1002];
deque<pair<int, int> > d;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  int L = 0;
  int C = 0;
  for (int i = 1; i <= n; ++i) cin >> (A[i] + 1);
  int L2 = 0;
  int C2 = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (A[i][j] == 'E') L = i, C = j;
      if (A[i][j] == 'S') L2 = i, C2 = j;
    }
  viz[L][C] = 1;
  d.push_back({L, C});
  while (!d.empty()) {
    int a = d[0].first;
    int b = d[0].second;
    d.pop_front();
    for (int i = 0; i <= 3; ++i) {
      int LL = a + ox[i];
      int CC = b + oy[i];
      if (A[LL][CC] == 'T') continue;
      if ((LL >= 1 && LL <= n && CC >= 1 && CC <= m) && !viz[LL][CC]) {
        viz[LL][CC] = 1;
        lene[LL][CC] = lene[a][b] + 1;
        d.push_back({LL, CC});
      }
    }
  }
  int dmax = lene[L2][C2];
  int sol = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (('0' <= A[i][j] && A[i][j] <= '9') &&
          (1 <= lene[i][j] && lene[i][j] <= dmax))
        sol += (A[i][j] - '0');
  cout << sol;
  return 0;
}
