#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 7;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
int n, m, fx, fy, ans;
int d[N][N];
char a[N][N];
bool used[N][N];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue<pair<int, int> > q;
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'E') {
        q.push({i, j});
        used[i][j] = 1;
      }
      if (a[i][j] == 'S') {
        fx = i;
        fy = j;
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int px = x + dx[i];
      int py = y + dy[i];
      if (!used[px][py] && a[px][py] != 'T' && min(px, py) >= 1 && px <= n &&
          py <= m) {
        used[px][py] = 1;
        q.push({px, py});
        d[px][py] = d[x][y] + 1;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][j] == 'T' || a[i][j] == 'E' || a[i][j] == 'S') continue;
      if (d[i][j] <= d[fx][fy] && d[i][j]) {
        ans += a[i][j] - '0';
      }
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  while (tt--) {
    solve();
  }
}
