#include <bits/stdc++.h>
using namespace std;
int r, c, pr, pc, er, ec, answer;
char s[1100][1100];
int mor[4] = {0, 0, 1, -1};
int moc[4] = {1, -1, 0, 0};
int step[1100][1100];
void bfs() {
  queue<pair<int, int>> q;
  q.push(make_pair(er, ec));
  while (q.empty() == false) {
    int cx = q.front().first, cy = q.front().second;
    q.pop();
    if (cx < 0 || cx >= r || cy < 0 || cy >= c || s[cx][cy] == 'T') continue;
    for (int i = 0; i < 4; i++) {
      int nx = cx + mor[i], ny = cy + moc[i];
      if (nx < 0 || nx >= r || ny < 0 || ny >= c || s[nx][ny] == 'T') continue;
      if (step[nx][ny]) continue;
      step[nx][ny] = step[cx][cy] + 1;
      q.push(make_pair(nx, ny));
    }
  }
  return;
}
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> s[i];
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (s[i][j] == 'S')
        pr = i, pc = j;
      else if (s[i][j] == 'E')
        er = i, ec = j;
  bfs();
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if ('1' <= s[i][j] && s[i][j] <= '9' && step[i][j] <= step[pr][pc] &&
          step[i][j] != 0)
        answer += s[i][j] - '0';
    }
  }
  cout << answer << '\n';
  return 0;
}
