#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  long long i, j;
  char ch[n][m];
  long long val[n][m];
  long long vis[n][m];
  long long x1, y1, x2, y2;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> ch[i][j];
      val[i][j] = 0;
      vis[i][j] = 0;
      if (ch[i][j] == 'E') {
        x1 = i;
        y1 = j;
      }
      if (ch[i][j] == 'S') {
        x2 = i;
        y2 = j;
      }
    }
  }
  queue<pair<pair<long long, long long>, long long>> q;
  q.push({{x1, y1}, 0});
  while (!q.empty()) {
    pair<pair<long long, long long>, long long> qe = q.front();
    i = qe.first.first;
    j = qe.first.second;
    q.pop();
    if (vis[i][j] == 0) {
      val[i][j] = qe.second;
      long long di[] = {0, 0, -1, 1};
      long long dr[] = {-1, 1, 0, 0};
      for (long long k = 0; k < 4; k++) {
        if (i + di[k] >= 0 && i + di[k] < n && j + dr[k] >= 0 &&
            j + dr[k] < m && ch[i + di[k]][j + dr[k]] != 'T') {
          q.push({{i + di[k], j + dr[k]}, qe.second + 1});
        }
      }
    }
    vis[i][j] = 1;
  }
  long long ans = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (ch[i][j] != 'T' && ch[i][j] != 'S' && ch[i][j] != 'E' &&
          val[i][j] <= val[x2][y2] && val[i][j] != 0) {
        ans += (long long)(ch[i][j] - '0');
      }
    }
  }
  cout << ans;
}
