#include <bits/stdc++.h>
using namespace std;
string rows[1000];
int dist[1000][1000];
bool disc[1000][1000];
int r, c;
void bfs(int row, int col) {
  queue<pair<int, int> > q;
  q.push(make_pair(row, col));
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    disc[cur.first][cur.second] = true;
    q.pop();
    if (cur.first > 0 && !disc[cur.first - 1][cur.second] &&
        rows[cur.first - 1][cur.second] != 'T') {
      dist[cur.first - 1][cur.second] = dist[cur.first][cur.second] + 1;
      disc[cur.first - 1][cur.second] = true;
      q.push(make_pair(cur.first - 1, cur.second));
    }
    if (cur.first < r - 1 && !disc[cur.first + 1][cur.second] &&
        rows[cur.first + 1][cur.second] != 'T') {
      dist[cur.first + 1][cur.second] = dist[cur.first][cur.second] + 1;
      disc[cur.first + 1][cur.second] = true;
      q.push(make_pair(cur.first + 1, cur.second));
    }
    if (cur.second < c - 1 && !disc[cur.first][cur.second + 1] &&
        rows[cur.first][cur.second + 1] != 'T') {
      dist[cur.first][cur.second + 1] = dist[cur.first][cur.second] + 1;
      disc[cur.first][cur.second + 1] = true;
      q.push(make_pair(cur.first, cur.second + 1));
    }
    if (cur.second > 0 && !disc[cur.first][cur.second - 1] &&
        rows[cur.first][cur.second - 1] != 'T') {
      dist[cur.first][cur.second - 1] = dist[cur.first][cur.second] + 1;
      disc[cur.first][cur.second - 1] = true;
      q.push(make_pair(cur.first, cur.second - 1));
    }
  }
}
int main(void) {
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> rows[i];
  memset(disc, 0, sizeof(disc));
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (rows[i][j] == 'E') bfs(i, j);
  pair<int, int> start;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (rows[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  int kiek = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (isdigit(rows[i][j]) &&
          dist[i][j] <= dist[start.first][start.second] && disc[i][j]) {
        kiek += int(rows[i][j] - '0');
      }
    }
  }
  cout << kiek << endl;
}
