#include <bits/stdc++.h>
using namespace std;
bool vis[3000][3000] = {0};
long long int dp[2000][2000] = {0};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n, m;
  cin >> n >> m;
  long long int arr[n + 2][m + 2];
  for (long long int i = 0; i <= n + 1; i++) {
    for (long long int j = 0; j <= m + 1; j++) {
      arr[i][j] = -1;
    }
  }
  long long int posa, posb, s, d;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= m; j++) {
      char x;
      cin >> x;
      if (x == 'T') {
        vis[i][j] = 1;
        continue;
      }
      arr[i][j] = 0;
      if (x == 'S') {
        s = i;
        d = j;
        continue;
      }
      if (x == 'E') {
        posa = i;
        posb = j;
        continue;
      }
      arr[i][j] = x - '0';
    }
  }
  for (long long int i = 0; i <= n + 1; i++) {
    for (long long int j = 0; j <= m + 1; j++) {
      if (arr[i][j] == -1) vis[i][j] = 1;
    }
  }
  queue<pair<long long int, long long int>> crr;
  crr.push({posa, posb});
  vis[posa][posb] = 1;
  while (!crr.empty()) {
    long long int a = crr.front().first;
    long long int b = crr.front().second;
    crr.pop();
    if (vis[a + 1][b] == 0) {
      dp[a + 1][b] = dp[a][b] + 1;
      vis[a + 1][b] = 1;
      crr.push({a + 1, b});
    }
    if (vis[a][b - 1] == 0) {
      dp[a][b - 1] = dp[a][b] + 1;
      vis[a][b - 1] = 1;
      crr.push({a, b - 1});
    }
    if (vis[a - 1][b] == 0) {
      dp[a - 1][b] = dp[a][b] + 1;
      vis[a - 1][b] = 1;
      crr.push({a - 1, b});
    }
    if (vis[a][b + 1] == 0) {
      dp[a][b + 1] = dp[a][b] + 1;
      vis[a][b + 1] = 1;
      crr.push({a, b + 1});
    }
  }
  long long int ans = dp[s][d], cnt = 0;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= m; j++) {
      if (arr[i][j] <= 0) continue;
      if (dp[i][j] <= ans && dp[i][j] > 0) {
        cnt += arr[i][j];
      }
    }
  }
  cout << cnt;
}
