#include <bits/stdc++.h>
using namespace std;
bool comparator(string a, string b) {
  if (a.size() == b.size()) {
    return a > b;
  }
  return a.size() > b.size();
}
int main() {
  int n, m;
  cin >> n >> m;
  string mat[n];
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
  }
  vector<pair<int, int> > enemy;
  vector<int> adj[n * m];
  int jugador;
  int end;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int pos = i * m + j;
      if (mat[i][j] == 'T') continue;
      if (mat[i][j] == 'E') {
        end = pos;
      }
      if (mat[i][j] - '0' >= 0 && mat[i][j] - '0' <= 9) {
        enemy.emplace_back(pair<int, int>(pos, mat[i][j] - '0'));
      }
      if (mat[i][j] == 'S') {
        jugador = pos;
      }
      if (i > 0 && mat[i - 1][j] != 'T') {
        adj[pos].emplace_back(pos - m);
        adj[pos - m].emplace_back(pos);
      }
      if (i < n - 1 && mat[i + 1][j] != 'T') {
        adj[pos].emplace_back(pos + m);
        adj[pos + m].emplace_back(pos);
      }
      if (j < m - 1 && mat[i][j + 1] != 'T') {
        adj[pos].emplace_back(pos + 1);
        adj[pos + 1].emplace_back(pos);
      }
      if (j > 0 && mat[i][j - 1] != 'T') {
        adj[pos].emplace_back(pos - 1);
        adj[pos - 1].emplace_back(pos);
      }
    }
  }
  queue<int> qu;
  int dist[n * m];
  for (int i = 0; i < n * m; i++) {
    dist[i] = 1000000000;
  }
  dist[end] = 0;
  qu.push(end);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (auto v : adj[u]) {
      if (dist[u] + 1 < dist[v]) {
        dist[v] = dist[u] + 1;
        qu.push(v);
      }
    }
  }
  long long ans = 0;
  for (auto ene : enemy) {
    if (dist[ene.first] <= dist[jugador]) {
      ans += ene.second;
    }
  }
  cout << ans;
}
