#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 1005;
const int inf = 1 << 26;
string s[N];
int dis[N][N];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool isok(int x, int y) {
  return (x >= 0 && x < n && y >= 0 && y < m && s[x][y] != 'T');
}
bool isDigit(char c) { return (c >= '0' && c <= '9'); }
queue<pair<int, int> > q;
bool vis[N][N];
void bfs(int x, int y) {
  while (!q.empty()) {
    q.pop();
  }
  q.push(make_pair(x, y));
  vis[x][y] = 1;
  while (!q.empty()) {
    pair<int, int> temp = q.front();
    q.pop();
    int a = temp.first;
    int b = temp.second;
    for (int i = 0; i < 4; i++) {
      int tx = a + dx[i];
      int ty = b + dy[i];
      if (isok(tx, ty) && !vis[tx][ty]) {
        dis[tx][ty] = dis[a][b] + 1;
        q.push(make_pair(tx, ty));
        vis[tx][ty] = 1;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  int st, e, x, y;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      dis[i][j] = inf;
      if (s[i][j] == 'S') {
        st = i;
        e = j;
      } else if (s[i][j] == 'E') {
        x = i;
        y = j;
        dis[i][j] = 0;
      }
    }
  }
  bfs(x, y);
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (dis[i][j] <= dis[st][e] && isDigit(s[i][j])) {
        ans += s[i][j] - '0';
      }
  cout << ans << endl;
  return 0;
}
