#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000 + 10;
char a[MAX][MAX];
vector<pair<pair<int, int>, int> > b;
int dis[MAX][MAX];
bool mark[MAX][MAX];
int n, m;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
void BFS(int z, int w) {
  queue<pair<int, int> > q;
  q.push({z, w});
  dis[z][w] = 0;
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    int x = cur.first;
    int y = cur.second;
    for (int i = 0; i < 4; i++) {
      int curx = x + dx[i];
      int cury = y + dy[i];
      if (curx <= n && curx >= 1 && cury <= m && cury >= 1 &&
          mark[curx][cury] == false && a[curx][cury] != 'T') {
        mark[curx][cury] = true;
        dis[curx][cury] = dis[x][y] + 1;
        q.push({curx, cury});
      }
    }
  }
}
int main() {
  pair<int, int> exit;
  pair<int, int> start;
  cin >> n >> m;
  int ans = 0;
  memset(dis, -1, sizeof(dis));
  for (int i = 1; i <= n; i++) {
    string curr;
    cin >> curr;
    for (int j = 0; j < curr.size(); j++) {
      a[i][j + 1] = curr[j];
      if (a[i][j + 1] >= '0' && a[i][j + 1] <= '9') {
        int num = int(a[i][j + 1]) - '0';
        b.push_back({{i, j + 1}, num});
      } else if (a[i][j + 1] == 'E') {
        exit.first = i;
        exit.second = j + 1;
      } else if (a[i][j + 1] == 'S') {
        start.first = i;
        start.second = j + 1;
      }
    }
  }
  BFS(exit.first, exit.second);
  for (int i = 0; i < b.size(); i++) {
    int a = b[i].first.first;
    int c = b[i].first.second;
    int d = b[i].second;
    if (dis[a][c] != -1 && dis[a][c] <= dis[start.first][start.second]) {
      ans += d;
    }
  }
  cout << ans << endl;
}
