#include <bits/stdc++.h>
using namespace std;
int r, c;
vector<string> Map;
int d[1010][1010];
bool vis[1010][1010];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int sx, sy, ex, ey;
queue<int> q;
void spfa() {
  memset(vis, false, sizeof(vis));
  vis[ex][ey] = true;
  q.push(ex * c + ey);
  while (!q.empty()) {
    int tmp = q.front();
    int x = tmp / c;
    int y = tmp % c;
    q.pop();
    vis[x][y] = false;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= 0 && nx < r && ny >= 0 && ny < c) {
        if (Map[nx][ny] != 'T') {
          if (d[nx][ny] > d[x][y] + 1) {
            d[nx][ny] = d[x][y] + 1;
            if (!vis[nx][ny]) {
              vis[nx][ny] = true;
              q.push(nx * c + ny);
            }
          }
        }
      }
    }
  }
}
int main() {
  string s;
  while (cin >> r >> c) {
    for (int i = 0; i < r; ++i) {
      cin >> s;
      Map.push_back(s);
      for (int j = 0; j < c; ++j) {
        d[i][j] = 1000000000;
        if (Map[i][j] == 'S') {
          sx = i;
          sy = j;
        }
        if (Map[i][j] == 'E') {
          ex = i;
          ey = j;
        }
      }
    }
    d[ex][ey] = 0;
    spfa();
    int ans = 0;
    for (int i = 0; i < r; ++i)
      for (int j = 0; j < c; ++j)
        if (Map[i][j] >= '0' && Map[i][j] <= '9') {
          if (d[i][j] <= d[sx][sy]) ans += Map[i][j] - '0';
        }
    cout << ans << endl;
  }
  return 0;
}
