#include <bits/stdc++.h>
using namespace std;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230;
void swaps(char *x, char *y) {
  char temp;
  temp = *x;
  *x = *y;
  *y = temp;
}
void swapi(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a == 1 || b == 1) return 1;
  if (a == b) return a;
  if (a > b)
    return gcd(b, a % b);
  else
    return gcd(a, b % a);
}
char X[1001][1001];
int R, C;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dist[1001][1001];
struct state {
  int x, y, way, c;
  state(int _x, int _y, int _way, int _c) : x(_x), y(_y), way(_way), c(_c) {}
};
bool check(int x, int y) {
  return (x >= 0 && x < R && y >= 0 && y < C && X[x][y] != 'T');
}
int main() {
  int Cases = 1;
  for (int Case = 0; Case < Cases; Case++) {
    cin >> R >> C;
    int start_x, start_y, pos_x, pos_y;
    for (int i = 0; i < R; i++) {
      cin >> X[i];
      for (int j = 0; j < C; j++) {
        if (X[i][j] == 'E') {
          start_x = i;
          start_y = j;
        }
        if (X[i][j] == 'S') {
          pos_x = i;
          pos_y = j;
        }
      }
    }
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        dist[i][j] = (1 << 30);
      }
    }
    int x, y, way, c;
    queue<state> Q;
    Q.push(state(start_x, start_y, 4, 0));
    dist[start_x][start_y] = 0;
    while (!Q.empty()) {
      x = Q.front().x;
      y = Q.front().y;
      way = Q.front().way;
      c = Q.front().c;
      Q.pop();
      for (int i = 0; i < 4; i++) {
        int xi = x + dx[i];
        int yi = y + dy[i];
        if (check(xi, yi) && (dist[xi][yi] > c + 1)) {
          Q.push(state(xi, yi, i, c + 1));
          dist[xi][yi] = c + 1;
        }
      }
    }
    int ans = 0;
    int d = dist[pos_x][pos_y];
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        if (X[i][j] >= '1' && X[i][j] <= '9') {
          if (dist[i][j] <= d) ans += (X[i][j] - '0');
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
