#include <bits/stdc++.h>
using namespace std;
int n, m, r, c, sx, sy, ex, ey;
char ch[1010][1010];
int mp[1010][1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dist;
bool valid(int x, int y) {
  return 0 <= x && x < r && 0 <= y && y < c && mp[x][y] < 500000001;
}
void rec(int x, int y, int d) {
  if (mp[x][y] == -1) {
    mp[x][y] = d;
  } else if (mp[x][y] >= 500000001) {
    return;
  } else {
    if (mp[x][y] > d) {
      mp[x][y] = d;
    } else {
      return;
    }
  }
  for (int i = 0; i < 4; i++) {
    if (valid(x + dx[i], y + dy[i])) {
      rec(x + dx[i], y + dy[i], d + 1);
    }
  }
}
void bfs(int X, int Y) {
  queue<pair<int, int> > que;
  que.push(pair<int, int>(X, Y));
  mp[X][Y] = 0;
  int x, y;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    x = p.first;
    y = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (valid(nx, ny) && mp[nx][ny] == -1) {
        mp[nx][ny] = mp[x][y] + 1;
        que.push(pair<int, int>(nx, ny));
      }
    }
  }
}
int ans = 0;
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    cin >> ch[i];
    for (int j = 0; j < c; j++) {
      mp[i][j] = -1;
      if (ch[i][j] == 'T') {
        mp[i][j] = 500000001;
      } else if (ch[i][j] == 'S') {
        sx = i;
        sy = j;
      } else if (ch[i][j] == 'E') {
        ex = i;
        ey = j;
      }
    }
  }
  bfs(sx, sy);
  dist = mp[ex][ey];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mp[i][j] < 500000001) mp[i][j] = -1;
    }
  }
  bfs(ex, ey);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mp[i][j] <= dist && mp[i][j] != -1 && '1' <= ch[i][j] &&
          ch[i][j] <= '9') {
        ans += (int)(ch[i][j] - '0');
      }
    }
  }
  cout << ans << endl;
  return 0;
}
