#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int dirx[4] = {0, 0, 1, -1};
int diry[4] = {1, -1, 0, 0};
int r, c;
bool ispossible(int x, int y) {
  if (x >= 0 && x < r && y >= 0 && y < c) {
    return true;
  }
  return false;
}
char mat[N][N];
int dist[N][N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int ans = 0;
  cin >> r >> c;
  int x = 0, y = 0;
  int sx, sy;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> mat[i][j];
      dist[i][j] = INT_MAX;
      if (mat[i][j] == 'S') {
        x = i;
        y = j;
      }
      if (mat[i][j] == 'E') {
        sx = i;
        sy = j;
      }
    }
  }
  dist[sx][sy] = 0;
  queue<pair<int, int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    pair<int, int> p = q.front();
    int a = p.first;
    int b = p.second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (ispossible(a + dirx[i], b + diry[i])) {
        if (mat[a + dirx[i]][b + diry[i]] == 'T' ||
            dist[a + dirx[i]][b + diry[i]] != INT_MAX) {
          continue;
        }
        q.push({a + dirx[i], b + diry[i]});
        dist[a + dirx[i]][b + diry[i]] = dist[a][b] + 1;
      }
    }
  }
  int req = dist[x][y];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mat[i][j] == 'T' || mat[i][j] == 'E' || mat[i][j] == 'S') {
        continue;
      }
      if (dist[i][j] <= req) {
        int no = mat[i][j] - '0';
        ans += no;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
