#include <bits/stdc++.h>
using namespace std;
int infinito = 1000000000;
int movi[] = {-1, 1, 0, 0};
int movj[] = {0, 0, -1, 1};
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<string> v(n + 2, string(m + 2, 'T'));
  vector<vector<int> > ocupado(n + 2, vector<int>(m + 2, 1));
  int iini, jini, ifin, jfin;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> v[i][j];
      if (v[i][j] != 'T') ocupado[i][j] = 0;
      if (v[i][j] == 'E') {
        ifin = i;
        jfin = j;
      }
      if (v[i][j] == 'S') {
        iini = i;
        jini = j;
      }
    }
  }
  queue<pair<int, int> > q;
  vector<vector<int> > dist(n + 2, vector<int>(m + 2, infinito));
  q.push(pair<int, int>(ifin, jfin));
  dist[ifin][jfin] = 0;
  int d;
  while (not(q.empty())) {
    pair<int, int> p = q.front();
    q.pop();
    int i = p.first;
    int j = p.second;
    for (int k = 0; k < 4; k++) {
      int nexti = i + movi[k];
      int nextj = j + movj[k];
      if (not ocupado[nexti][nextj] and dist[nexti][nextj] == infinito) {
        dist[nexti][nextj] = dist[i][j] + 1;
        q.push(pair<int, int>(nexti, nextj));
        if (nexti == iini and nextj == jini) {
          d = dist[nexti][nextj];
        }
      }
    }
  }
  int amount = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dist[i][j] <= d and v[i][j] >= '0' and v[i][j] <= '9') {
        amount += v[i][j] - '0';
      }
    }
  }
  cout << amount << endl;
}
