#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MAX = INT_MAX / 10;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int main() {
  int r, c;
  cin >> r >> c;
  vector<string> f(r);
  pair<int, int> start, goal;
  for (int i = (0); i < (r); ++i) {
    cin >> f[i];
    for (int j = (0); j < (c); ++j) {
      if (f[i][j] == 'S') {
        start = make_pair(i, j);
      }
      if (f[i][j] == 'E') {
        goal = make_pair(i, j);
      }
    }
  }
  queue<pair<int, int> > q;
  vector<vector<int> > cost(r, vector<int>(c, MAX));
  q.push(goal);
  cost[goal.first][goal.second] = 0;
  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int d = (0); d < (4); ++d) {
      int y = p.first + dy[d];
      int x = p.second + dx[d];
      if (y >= 0 && x >= 0 && y < r && x < c && cost[y][x] == MAX &&
          f[y][x] != 'T') {
        q.push(make_pair(y, x));
        cost[y][x] = cost[p.first][p.second] + 1;
      }
    }
  }
  int ans = 0;
  for (int i = (0); i < (r); ++i) {
    for (int j = (0); j < (c); ++j) {
      if (cost[i][j] <= cost[start.first][start.second] && isdigit(f[i][j])) {
        ans += f[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
}
