#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<string> d(n);
  for (int i = 0; i < n; ++i) cin >> d[i];
  bool v[1000][1000] = {0};
  vector<vector<int> > p;
  p.resize(n);
  for (int i = 0; i < n; ++i) p[i].resize(m);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) p[i][j] = 100000000;
  ;
  ;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) v[i][j] = false;
  ;
  int sx, sy, ex, ey;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (d[i][j] == 'E')
        ex = i, ey = j;
      else if (d[i][j] == 'S')
        sx = i, sy = j;
  p[ex][ey] = 0;
  queue<pair<int, int> > q;
  q.push(make_pair(ex, ey));
  while (!q.empty()) {
    pair<int, int> c = q.front();
    q.pop();
    int x = c.first, y = c.second;
    int cr = p[x][y] + 1;
    if (x + 1 < n && d[x + 1][y] != 'T' && !v[x + 1][y]) {
      p[x + 1][y] = cr;
      v[x + 1][y] = true;
      q.push(make_pair(x + 1, y));
    }
    if (x - 1 >= 0 && d[x - 1][y] != 'T' && !v[x - 1][y]) {
      p[x - 1][y] = cr;
      v[x - 1][y] = true;
      q.push(make_pair(x - 1, y));
    }
    if (y + 1 < m && d[x][y + 1] != 'T' && !v[x][y + 1]) {
      p[x][y + 1] = cr;
      v[x][y + 1] = true;
      q.push(make_pair(x, y + 1));
    }
    if (y - 1 >= 0 && d[x][y - 1] != 'T' && !v[x][y - 1]) {
      p[x][y - 1] = cr;
      v[x][y - 1] = true;
      q.push(make_pair(x, y - 1));
    }
  }
  int ret = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (p[i][j] <= p[sx][sy] && d[i][j] != 'T' && d[i][j] != 'S' &&
          d[i][j] != 'E') {
        ret += d[i][j] - '0';
      }
  cout << ret << endl;
}
