#include <bits/stdc++.h>
using namespace std::chrono;
using namespace std;
const int mod = 1e9 + 7;
void solve() {
  int n, m;
  cin >> n >> m;
  string s[n];
  for (int i = 0; i < n; ++i) cin >> s[i];
  pair<int, int> sr, e;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') {
        sr.first = i;
        sr.second = j;
      }
      if (s[i][j] == 'E') {
        e.first = i;
        e.second = j;
      }
    }
  }
  queue<pair<int, int> > q;
  q.push(e);
  int dist[n][m];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) dist[i][j] = INT_MAX;
  dist[e.first][e.second] = 0;
  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();
    int i = p.first;
    int j = p.second;
    if (i + 1 < n && dist[i + 1][j] > 1 + dist[i][j] && s[i + 1][j] != 'T') {
      dist[i + 1][j] = dist[i][j] + 1;
      q.push({i + 1, j});
    }
    if (j + 1 < m && dist[i][j + 1] > dist[i][j] + 1 && s[i][j + 1] != 'T') {
      dist[i][j + 1] = 1 + dist[i][j];
      q.push({i, j + 1});
    }
    if (i - 1 >= 0 && dist[i - 1][j] > 1 + dist[i][j] && s[i - 1][j] != 'T') {
      dist[i - 1][j] = 1 + dist[i][j];
      q.push({i - 1, j});
    }
    if (j - 1 >= 0 && dist[i][j - 1] > 1 + dist[i][j] && s[i][j - 1] != 'T') {
      dist[i][j - 1] = 1 + dist[i][j];
      q.push({i, j - 1});
    }
  }
  int t = dist[sr.first][sr.second], ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] >= '0' && s[i][j] <= '9' && dist[i][j] <= t) {
        ans += s[i][j] - '0';
      }
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) solve();
}
