#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MOD = 1000000007;
const long long MAXN = 200005;
const double eps = 1e-6;
long long n, m, vis[1005][1005], sx, sy;
char mp[1005][1005];
long long movx[5] = {0, 0, 0, -1, 1};
long long movy[5] = {0, -1, 1, 0, 0};
bool legal(long long x, long long y) {
  if (mp[x][y] == 'T') {
    return false;
  }
  if (x > n || x <= 0) {
    return false;
  }
  if (y > m || y <= 0) {
    return false;
  }
  return true;
}
int main() {
  while (~scanf("%lld %lld", &n, &m)) {
    for (long long i = 1; i <= n; i++) {
      scanf("%s", mp[i] + 1);
      for (long long j = 1; j <= m; j++) {
        if (mp[i][j] == 'E') {
          sx = i;
          sy = j;
        }
      }
    }
    memset(vis, -1, sizeof(vis));
    queue<pair<long long, long long> > q;
    q.push(make_pair(sx, sy));
    vis[sx][sy] = 0;
    while (!q.empty()) {
      pair<long long, long long> node = q.front();
      long long ux = node.first;
      long long uy = node.second;
      q.pop();
      for (long long i = 1; i <= 4; i++) {
        long long nx = node.first + movx[i];
        long long ny = node.second + movy[i];
        if ((legal(nx, ny)) && (vis[nx][ny] == -1)) {
          vis[nx][ny] = vis[ux][uy] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
    long long len;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        if (mp[i][j] == 'S') {
          len = vis[i][j] + 1;
        }
      }
    }
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        if ((isdigit(mp[i][j])) && (vis[i][j] < len) && (vis[i][j] != -1)) {
          ans += mp[i][j] - '0';
        }
      }
    }
    printf("%lld\n", ans);
  }
}
