#include <bits/stdc++.h>
using namespace std;
char grid[1005][1005];
int step[1005][1005];
int visited[1005][1005];
int n, m;
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
struct point {
  int x, y;
  point(int x, int y) {
    this->x = x;
    this->y = y;
  }
};
bool valid(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m) return 1;
  return 0;
}
void bfs(point start) {
  queue<point> q;
  q.push(start);
  visited[start.x][start.y] = 1;
  step[start.x][start.y] = 0;
  while (!q.empty()) {
    point u = q.front();
    q.pop();
    int x = u.x, y = u.y;
    for (int i = 0; i < 4; ++i) {
      int nx = x + X[i];
      int ny = y + Y[i];
      if (valid(nx, ny) && grid[nx][ny] != 'T') {
        if (!visited[nx][ny]) {
          q.push(point(nx, ny));
          visited[nx][ny] = 1;
          step[nx][ny] = step[x][y] + 1;
        }
      }
    }
  }
}
void solve() {
  cin >> n >> m;
  int startx, starty;
  for (int i = 0; i < 1005; ++i) {
    for (int j = 0; j < 1005; ++j) {
      step[i][j] = 0;
      visited[i][j] = 0;
    }
  }
  int initx, inity;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> grid[i][j];
      if (grid[i][j] == 'E') {
        startx = i;
        starty = j;
      }
      if (grid[i][j] == 'S') {
        initx = i;
        inity = j;
      }
    }
  }
  bfs(point(startx, starty));
  int playerstep = step[initx][inity];
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (step[i][j] <= playerstep && step[i][j] != 0) {
        if (isdigit(grid[i][j])) {
          ans += grid[i][j] - '0';
        }
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}
