#include <bits/stdc++.h>
using namespace std;
const int inf = 2147483647;
const long long llinf = 9223372036854775807LL;
const int MAX = 1001;
int field[MAX][MAX];
bool used[MAX][MAX];
int dist[MAX][MAX];
int xx[4] = {1, -1, 0, 0};
int yy[4] = {0, 0, 1, -1};
int r, c;
bool check(int x, int y) {
  return (x >= 0 && x < r && y >= 0 && y < c && field[x][y] != -1);
}
int main() {
  cin >> r >> c;
  int fx, fy;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      char ch;
      cin >> ch;
      if (ch == 'T')
        field[i][j] = -1;
      else if (ch == 'E') {
        field[i][j] = 105;
        fx = i;
        fy = j;
      } else if (ch == 'S')
        field[i][j] = 555;
      else
        field[i][j] = ch - '0';
    }
  dist[fx][fy] = 0;
  queue<int> q;
  q.push(fx);
  q.push(fy);
  used[fx][fy] = true;
  int lng = -1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int mx = x + xx[i];
      int my = y + yy[i];
      if (check(mx, my) && !used[mx][my]) {
        dist[mx][my] = dist[x][y] + 1;
        used[mx][my] = true;
        q.push(mx);
        q.push(my);
        if (field[mx][my] == 555) lng = dist[mx][my];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (field[i][j] > 0 && field[i][j] < 10 && dist[i][j] > 0 &&
          dist[i][j] <= lng)
        ans += field[i][j];
  cout << ans;
  return 0;
}
