#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, c;
  cin >> r >> c;
  vector<string> grid;
  for (int i = 0; i < r; i++) {
    string r1;
    cin >> r1;
    grid.push_back(r1);
  }
  pair<int, int> st;
  bool visited[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      visited[i][j] = false;
      if (grid[i][j] == 'E') {
        st = {i, j};
      }
    }
  }
  queue<pair<int, int> > pq;
  pq.push(st);
  bool found = false;
  int enemies = 0;
  while (!pq.empty()) {
    int s = pq.size();
    while (s--) {
      pair<int, int> q = pq.front();
      pq.pop();
      int i = q.first;
      int j = q.second;
      if (!visited[i][j]) {
        visited[i][j] = true;
        if (grid[i][j] == 'S')
          found = true;
        else if (grid[i][j] != 'E') {
          int num = grid[i][j] - '0';
          enemies += num;
        }
        if (i + 1 < r && !visited[i + 1][j] && grid[i + 1][j] != 'T') {
          pq.push({i + 1, j});
        }
        if (j + 1 < c && !visited[i][j + 1] && grid[i][j + 1] != 'T') {
          pq.push({i, j + 1});
        }
        if (i - 1 >= 0 && !visited[i - 1][j] && grid[i - 1][j] != 'T') {
          pq.push({i - 1, j});
        }
        if (j - 1 >= 0 && !visited[i][j - 1] && grid[i][j - 1] != 'T') {
          pq.push({i, j - 1});
        }
      }
    }
    if (found) {
      cout << enemies << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
