#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, c;
  cin >> r >> c;
  pair<int, int> exit, start;
  char grid[1000][1000];
  char cur;
  for (int y = 0; y < r; y++) {
    for (int x = 0; x < c; x++) {
      cin >> cur;
      grid[y][x] = cur;
      if (cur == 'E') exit = make_pair(x, y);
      if (cur == 'S') start = make_pair(x, y);
    }
  }
  vector<pair<int, int> > current, newpts;
  current.push_back(exit);
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  int out = 0;
  bool done = false;
  while (!done) {
    newpts.clear();
    for (int i = 0; i < current.size(); i++) {
      pair<int, int> pt = current[i];
      char* loc = &grid[pt.second][pt.first];
      char ch = *loc;
      if ('0' <= ch && ch <= '9') {
        out += ch - '0';
        *loc = 'X';
      }
      if (ch == 'S') {
        done = true;
      }
      if (ch != 'T' && ch != 'X') {
        for (int j = 0; j < 4; j++) {
          int nx = pt.first + dx[j], ny = pt.second + dy[j];
          if (nx >= 0 && nx < c && ny >= 0 && ny < r) {
            newpts.push_back(make_pair(nx, ny));
          }
        }
      }
    }
    current.swap(newpts);
  }
  cout << out;
  return 0;
}
