#include <bits/stdc++.h>
using namespace std;
int x[] = {-1, 0, 1, 0}, y[] = {0, 1, 0, -1};
vector<vector<int> > r(1002, vector<int>(1002, -1));
vector<vector<bool> > b(1001, vector<bool>(1001, false));
void izla(int i, int j) {
  queue<pair<int, int> > q;
  q.push(make_pair(i, j));
  b[i][j] = true;
  vector<int> d(1e6 + 1);
  while (!q.empty()) {
    pair<int, int> v = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      pair<int, int> u = make_pair(v.first + x[i], v.second + y[i]);
      if (r[u.first][u.second] == -2) {
        r[u.first][u.second] = r[v.first][v.second] + 1;
        q.push(u);
      }
    }
  }
}
int main() {
  int n, m, ib, jb, it, jt;
  char c;
  cin >> n >> m;
  vector<short> h;
  vector<pair<int, int> > nin;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c;
      switch (c) {
        case 'E':
          ib = i, jb = j;
          r[i][j] = 0;
          break;
        case 'S':
          it = i, jt = j;
          r[i][j] = -2;
          break;
        case 'T':
          break;
        case '0':
          r[i][j] = -2;
          break;
        default:
          nin.push_back(make_pair(i, j));
          h.push_back(c - 48);
          r[i][j] = -2;
      }
    }
  izla(ib, jb);
  int t = r[it][jt];
  int second = 0;
  for (int i = 0; i < nin.size(); i++)
    if (r[nin[i].first][nin[i].second] <= t &&
        r[nin[i].first][nin[i].second] > 0)
      second += h[i];
  cout << second;
}
