#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 1001;
void zero(int S[][MAX_SIZE]);
pair<int, int> read_map(char Map[][MAX_SIZE], const int& n, const int& m);
int Status[MAX_SIZE][MAX_SIZE];
int Path[MAX_SIZE][MAX_SIZE];
int main() {
  char Map[MAX_SIZE][MAX_SIZE];
  int n, m;
  cin >> n >> m;
  zero(Status);
  zero(Path);
  pair<int, int> start;
  queue<pair<int, int> > q;
  start = read_map(Map, n, m);
  Status[start.first][start.second] = 1;
  q.push(start);
  int dirX[4] = {1, 0, -1, 0};
  int dirY[4] = {0, 1, 0, -1};
  int count = 0;
  int f = -1;
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    if (f != -1 && Path[cur.first][cur.second] > f) break;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int ii = cur.first + dirX[i], jj = cur.second + dirY[i];
      if (ii >= 0 && ii < n && jj >= 0 && jj < m && !Status[ii][jj] &&
          Map[ii][jj] != 'T') {
        q.push({ii, jj});
        Status[ii][jj] = 1;
        Path[ii][jj] = Path[cur.first][cur.second] + 1;
        if (Map[ii][jj] != 'S' && (Path[ii][jj] == f || f == -1)) {
          count += Map[ii][jj] - '0';
        } else if (f == -1)
          f = Path[ii][jj];
      }
    }
  }
  cout << count;
  return 0;
}
pair<int, int> read_map(char Map[][MAX_SIZE], const int& n, const int& m) {
  int si, sj;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> Map[i][j];
      if (Map[i][j] == 'E') {
        si = i;
        sj = j;
      }
    }
  }
  return {si, sj};
}
void zero(int S[][MAX_SIZE]) {
  for (int i = 0; i < MAX_SIZE; ++i) {
    for (int j = 0; j < MAX_SIZE; ++j) S[i][j] = 0;
  }
}
