#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> e, s;
string grid[1001];
vector<pair<int, int> > enemies;
int level[1001][1001];
int fx[] = {0, 1, 0, -1};
int fy[] = {1, 0, -1, 0};
bool valid(int x, int y) {
  return x >= 0 and x < n and y >= 0 and y < m and grid[x][y] != 'T' and
         level[x][y] == -1;
}
void print() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (level[i][j] > 0) cout << ' ';
      cout << level[i][j] << ' ';
    }
    cout << '\n';
  }
  return;
}
void bfs(pair<int, int> v) {
  memset(level, -1, sizeof level);
  queue<pair<int, int> > q;
  q.push(v);
  level[v.first][v.second] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + fx[i];
      int ny = y + fy[i];
      if (valid(nx, ny)) {
        level[nx][ny] = level[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == 'E')
        e = {i, j};
      else if (grid[i][j] == 'S')
        s = {i, j};
      else if (isdigit(grid[i][j]) and grid[i][j] != '0')
        enemies.push_back({i, j});
    }
  }
  bfs(e);
  int step = level[s.first][s.second];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (isdigit(grid[i][j]) and grid[i][j] != '0' and level[i][j] <= step and
          level[i][j] != -1)
        ans += grid[i][j] - '0';
    }
  }
  cout << ans << '\n';
  return 0;
}
