#include <bits/stdc++.h>
using namespace std;
const int maxn = 1002;
int n, r, c, dist[maxn][maxn], startx, starty, ENDX, ENDY, answer;
char matrix[maxn][maxn];
bool mark[maxn][maxn];
void bfs(int x, int y) {
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  mark[x][y] = true;
  while (q.size()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (matrix[x + 1][y] != 'T' && !mark[x + 1][y] && matrix[x + 1][y] != 'E') {
      dist[x + 1][y] = dist[x][y] + 1;
      mark[x + 1][y] = true;
      q.push(make_pair(x + 1, y));
    }
    if (matrix[x - 1][y] != 'T' && !mark[x - 1][y] && matrix[x - 1][y] != 'E') {
      dist[x - 1][y] = dist[x][y] + 1;
      mark[x - 1][y] = true;
      q.push(make_pair(x - 1, y));
    }
    if (matrix[x][y + 1] != 'T' && !mark[x][y + 1] && matrix[x][y + 1] != 'E') {
      dist[x][y + 1] = dist[x][y] + 1;
      mark[x][y + 1] = true;
      q.push(make_pair(x, y + 1));
    }
    if (matrix[x][y - 1] != 'T' && !mark[x][y - 1] && matrix[x][y - 1] != 'E') {
      dist[x][y - 1] = dist[x][y] + 1;
      mark[x][y - 1] = true;
      q.push(make_pair(x, y - 1));
    }
  }
}
int main() {
  cin >> r >> c;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      cin >> matrix[j][i];
      if (matrix[j][i] == 'E') {
        ENDX = j;
        ENDY = i;
      }
      if (matrix[j][i] == 'S') {
        startx = j;
        starty = i;
      }
    }
  for (int i = 0; i < maxn; i++)
    matrix[i][0] = 'T', matrix[0][i] = 'T', matrix[i][r + 1] = 'T',
    matrix[c + 1][i] = 'T';
  bfs(ENDX, ENDY);
  answer = 0;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      if (dist[startx][starty] >= dist[j][i] && matrix[j][i] != 'T' &&
          matrix[j][i] != 'E' && matrix[j][i] != 'S' && dist[j][i] != 0)
        answer = answer + (matrix[j][i] - '0');
    }
  cout << answer;
  return 0;
}
