#include <bits/stdc++.h>
using namespace std;
int distst[1100][1100];
int distbr[1100][1100];
queue<pair<int, int> > Q;
string str[1100];
int ex, ey;
int sx, sy;
int r, c;
bool check(int x, int y) {
  if (x < r && x >= 0 && y < c && y >= 0) {
    if (str[x][y] != 'T')
      return 1;
    else
      return 0;
  } else
    return 0;
}
void calcdist(int x, int y, int dist[1100][1100]) {
  int i, j;
  Q.push(pair<int, int>(x, y));
  pair<int, int> pt;
  int d;
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++) dist[i][j] = 1e8;
  dist[x][y] = 0;
  while (!Q.empty()) {
    pt = Q.front();
    Q.pop();
    x = pt.first;
    y = pt.second;
    d = dist[x][y];
    if (check(x + 1, y)) {
      if (dist[x + 1][y] > d + 1) {
        dist[x + 1][y] = d + 1;
        Q.push(pair<int, int>(x + 1, y));
      }
    }
    if (check(x, y + 1)) {
      if (dist[x][y + 1] > d + 1) {
        dist[x][y + 1] = d + 1;
        Q.push(pair<int, int>(x, y + 1));
      }
    }
    if (check(x - 1, y)) {
      if (dist[x - 1][y] > d + 1) {
        dist[x - 1][y] = d + 1;
        Q.push(pair<int, int>(x - 1, y));
      }
    }
    if (check(x, y - 1)) {
      if (dist[x][y - 1] > d + 1) {
        dist[x][y - 1] = d + 1;
        Q.push(pair<int, int>(x, y - 1));
      }
    }
  }
}
int main(int argc, const char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t, i, j, xt, yt, x, y;
  cin >> r >> c;
  for (i = 0; i < r; i++) {
    cin >> str[i];
    for (j = 0; j < c; j++) {
      if (str[i][j] == 'E') {
        ex = i;
        ey = j;
      } else if (str[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++) distst[i][j] = 1e8;
  static int minv = 0;
  calcdist(sx, sy, distst);
  calcdist(ex, ey, distbr);
  for (x = 0; x < r; x++) {
    for (y = 0; y < c; y++) {
      if (!(str[x][y] >= '1' && str[x][y] <= '9')) continue;
      if (distbr[x][y] <= distst[ex][ey]) minv += str[x][y] - '0';
    }
  }
  cout << minv;
  return 0;
}
