#include <bits/stdc++.h>
using namespace std;
int inf = 2147483647;
const long long ll = 9223372036854775807, ninf = 1000000000;
const double eps = 1e-6;
const long long nll = 223372036854775807;
string A[1005] = {};
int a, b;
bool vis[1005][1005] = {};
int f[1005][1005] = {};
void bfs(int i, int j) {
  queue<pair<int, int> > num;
  pair<int, int> t;
  t.first = i, t.second = j;
  num.push(t);
  f[t.first][t.second] = 0;
  while (!num.empty()) {
    pair<int, int> now = num.front();
    num.pop();
    int i = now.first, j = now.second;
    vis[now.first][now.second] = 1;
    if (i - 1 >= 0 && A[i - 1][j] != 'T' && !vis[i - 1][j] &&
        f[i - 1][j] > f[i][j] + 1) {
      f[i - 1][j] = f[i][j] + 1;
      pair<int, int> tmp;
      tmp.first = i - 1, tmp.second = j;
      num.push(tmp);
    }
    if (i + 1 < a && A[i + 1][j] != 'T' && !vis[i + 1][j] &&
        f[i + 1][j] > f[i][j] + 1) {
      f[i + 1][j] = f[i][j] + 1;
      pair<int, int> tmp;
      tmp.first = i + 1, tmp.second = j;
      num.push(tmp);
    }
    if (j - 1 >= 0 && A[i][j - 1] != 'T' && !vis[i][j - 1] &&
        f[i][j - 1] > f[i][j] + 1) {
      f[i][j - 1] = f[i][j] + 1;
      pair<int, int> tmp;
      tmp.first = i, tmp.second = j - 1;
      num.push(tmp);
    }
    if (j + 1 < b && A[i][j + 1] != 'T' && !vis[i][j + 1] &&
        f[i][j + 1] > f[i][j] + 1) {
      f[i][j + 1] = f[i][j] + 1;
      pair<int, int> tmp;
      tmp.first = i, tmp.second = j + 1;
      num.push(tmp);
    }
  }
}
int main() {
  while (cin >> a >> b) {
    for (int i = 0; i < a; i++) {
      cin >> A[i];
    }
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) f[i][j] = inf;
    int ans = 0;
    int now = 0;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (A[i][j] == 'E') {
          bfs(i, j);
        }
      }
    }
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (A[i][j] == 'S') now = f[i][j];
      }
    }
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (f[i][j] <= now && A[i][j] >= '1' && A[i][j] <= '9') {
          ans += A[i][j] - '0';
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
