#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T b, T p, T M) {
  long long int ret = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1) ret = (ret * b) % M;
    b = (b * b) % M;
  }
  return (T)ret;
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  return a * b / gcd<T>(a, b);
}
template <class T>
inline T power(T b, T p) {
  long long int x = 1;
  while (p--) x *= b;
  return x;
}
const long long int mod = 1e9 + 7;
const int siz = 202;
int dx[] = {+1, -1, +0, +0};
int dy[] = {+0, +0, +1, -1};
int r, c, aa, bb, xx, yy;
int level[1010][1010], vis[1010][1010];
char grid[1010][1010];
bool isValid(int x, int y) {
  if (x < 0 || x >= r || y < 0 || y >= c) return false;
  if (vis[x][y] || grid[x][y] == 'T') return false;
  return true;
}
void bfs() {
  queue<pair<int, int> > q;
  q.push({xx, yy});
  level[xx][yy] = 0;
  while (!q.empty()) {
    pair<int, int> pos = q.front();
    q.pop();
    if (pos.first == aa && pos.second == bb) return;
    for (int i = 0; i < 4; i++) {
      int tmpX = pos.first + dx[i];
      int tmpY = pos.second + dy[i];
      if (isValid(tmpX, tmpY)) {
        q.push({tmpX, tmpY});
        level[tmpX][tmpY] = level[pos.first][pos.second] + 1;
        vis[tmpX][tmpY] = 1;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  scanf("%d%d", &r, &c);
  for (int i = 0; i < r; i++) {
    scanf("%s", grid[i]);
    for (int j = 0; j < c; j++) {
      if (grid[i][j] == 'S') aa = i, bb = j;
      if (grid[i][j] == 'E') xx = i, yy = j;
    }
  }
  bfs();
  int myTime = level[aa][bb], ans = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (grid[i][j] >= '1' && grid[i][j] <= '9') {
        if (level[i][j] <= myTime && level[i][j] > 0) ans += grid[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
  return 0;
}
