#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 20;
string s[maxn];
int d[maxn][maxn], v[maxn][maxn], r, c, ssi, sssi, ssj, sssj, ans;
queue<pair<int, int> > tea;
void bfs(int u, int a) {
  d[u][a] = 0;
  tea.push(make_pair(u, a));
  v[u][a] = 1;
  while (!tea.empty()) {
    int ii = tea.front().first;
    int jj = tea.front().second;
    tea.pop();
    int fo[4] = {0, 1, -1, 0};
    int am[4] = {1, 0, 0, -1};
    for (int z = 0; z < 4; z++)
      if (ii + fo[z] >= 0 && jj + am[z] >= 0 && ii + fo[z] < r &&
          jj + am[z] < c)
        if (s[ii + fo[z]][jj + am[z]] != 'T')
          if (v[ii + fo[z]][jj + am[z]] == 0) {
            tea.push(make_pair(ii + fo[z], jj + am[z]));
            d[ii + fo[z]][jj + am[z]] = d[ii][jj] + 1;
            v[ii + fo[z]][jj + am[z]] = 1;
          }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> r >> c;
  memset(d, -1, sizeof(d));
  for (int i = 0; i < r; i++) cin >> s[i];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (s[i][j] == 'S') {
        ssi = i;
        ssj = j;
      }
      if (s[i][j] == 'E') {
        sssi = i;
        sssj = j;
      }
    }
  }
  bfs(sssi, sssj);
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (s[i][j] != 0 && s[i][j] != 'E' && s[i][j] != 'T' && s[i][j] != 'S' &&
          d[i][j] <= d[ssi][ssj] && d[i][j] != -1)
        ans += (int)(s[i][j] - '0');
  cout << ans << endl;
  return 0;
}
