#include <bits/stdc++.h>
using namespace std;
template <class T>
T sq(T n) {
  return n * n;
}
template <class T>
T getabs(T n) {
  return n < 0 ? -n : n;
}
template <class T>
T getmax(T a, T b) {
  return (a > b ? a : b);
}
template <class T>
T getmin(T a, T b) {
  return (a < b ? a : b);
}
template <class T>
void setmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
void setmin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
T gcd(T a, T b) {
  return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
  return ((a * b) / gcd(a, b));
}
template <class T>
T power(T n, T p) {
  if (p == 0) return 1;
  return ((p == 1) ? n : (n * power(n, p - 1)));
}
vector<int> pset(1000);
void initSet(int _size) {
  pset.resize(_size);
  for (__typeof(_size) i = 0; i < (_size); i++) pset[i] = i;
}
int findSet(int i) { return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); }
void unionSet(int i, int j) { pset[findSet(i)] = findSet(j); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
struct p {
  int x, y, d;
};
int n, m, ans;
char g[1005][1005];
bool vis[1005][1005];
vector<int> val, d;
int start;
void bfs(int x, int y, int now) {
  queue<p> q;
  p tmp, t, a;
  tmp.x = x;
  tmp.y = y;
  tmp.d = now;
  q.push(tmp);
  while (!q.empty()) {
    t = q.front();
    q.pop();
    if (vis[t.x][t.y]) continue;
    if (g[t.x][t.y] == 'T') continue;
    vis[t.x][t.y] = true;
    if (g[t.x][t.y] > '0' && g[t.x][t.y] <= '9') {
      d.push_back(t.d);
      val.push_back(g[t.x][t.y] - '0');
    }
    if (g[t.x][t.y] == 'S') start = t.d;
    if (t.x - 1 >= 0) {
      a.x = t.x - 1;
      a.y = t.y;
      a.d = t.d + 1;
      q.push(a);
    }
    if (t.y - 1 >= 0) {
      a.x = t.x;
      a.y = t.y - 1;
      a.d = t.d + 1;
      q.push(a);
    }
    if (t.x + 1 < n) {
      a.x = t.x + 1;
      a.y = t.y;
      a.d = t.d + 1;
      q.push(a);
    }
    if (t.y + 1 < m) {
      a.x = t.x;
      a.y = t.y + 1;
      a.d = t.d + 1;
      q.push(a);
    }
  }
}
int main() {
  while (scanf("%d %d", &n, &m) != -1) {
    gets(g[0]);
    for (__typeof(n) i = 0; i < (n); i++) gets(g[i]);
    memset((vis), 0, sizeof(vis));
    val.clear();
    d.clear();
    for (__typeof(n) i = 0; i < (n); i++) {
      for (__typeof(m) j = 0; j < (m); j++) {
        if (g[i][j] == 'E') {
          start = 0;
          bfs(i, j, 0);
          break;
        }
      }
    }
    ans = 0;
    for (__typeof(val.size()) i = 0; i < (val.size()); i++) {
      if (d[i] <= start) {
        ans += val[i];
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
