#include <bits/stdc++.h>
using namespace std;
int r, c, d[1020][1020], used[1020][1020];
string s[1020];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
long long ans = 0;
bool ok(int x, int y) {
  return x >= 0 && x < r && y >= 0 && y < c && s[x][y] != 'T';
}
void bfs(int x, int y) {
  used[x][y] = 1;
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  d[x][y] = 0;
  while (q.empty() == false) {
    int xk = q.front().first, yk = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = xk + dx[i];
      int yy = yk + dy[i];
      if (ok(xx, yy) == true && used[xx][yy] == 0) {
        used[xx][yy] = 1;
        d[xx][yy] = d[xk][yk] + 1;
        q.push(make_pair(xx, yy));
      }
    }
  }
}
int main() {
  scanf("%d %d", &r, &c);
  int sx, sy, ex, ey;
  for (int i = 0; i < r; i++) {
    cin >> s[i];
    for (int j = 0; j < c; j++) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
        s[i][j] = '0';
      } else if (s[i][j] == 'E') {
        ex = i;
        ey = j;
        s[i][j] = '0';
      }
    }
  }
  bfs(ex, ey);
  int need = d[sx][sy];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (s[i][j] == 'T' && i == sx && j == sy) continue;
      int add = (int)(s[i][j] - '0');
      if (d[i][j] <= need && d[i][j] != 0) {
        ans += add;
      }
    }
  }
  printf("%lld", ans);
  return 0;
}
