#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
template <typename T>
inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
  return (a * b) / gcd(a, b);
}
template <typename T>
string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}
template <class T>
void splitStr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
inline int two(int n) { return 1 << n; }
inline int isOnBit(int n, int b) { return (n >> b) & 1; }
inline void onBit(int &n, int b) { n |= two(b); }
inline void offBit(int &n, int b) { n &= ~two(b); }
inline int lastBit(int n) { return n & (-n); }
inline int cntBit(int n) {
  int res = 0;
  while (n && ++res) n -= n & (-n);
  return res;
}
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long modexp(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
char c[1007][1007];
long long a[1007][1007];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, x, y, x1, yyyyyy1;
  cin >> n >> m;
  for (long long i = 0, _n = (n); i < _n; i++) {
    for (long long j = 0, _n = (m); j < _n; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') {
        x1 = i;
        yyyyyy1 = j;
      }
      if (c[i][j] == 'E') {
        a[i][j] = 0;
        x = i;
        y = j;
      } else if (c[i][j] == 'T') {
        a[i][j] = -1;
      } else {
        a[i][j] = 10000000;
      }
    }
  }
  queue<pair<long long, long long>> q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    if (x + 1 < n && a[x + 1][y] != -1) {
      if (a[x + 1][y] > a[x][y] + 1) {
        a[x + 1][y] = a[x][y] + 1;
        q.push(make_pair(x + 1, y));
      }
    }
    if (x - 1 >= 0 && a[x - 1][y] != -1) {
      if (a[x - 1][y] > a[x][y] + 1) {
        a[x - 1][y] = a[x][y] + 1;
        q.push(make_pair(x - 1, y));
      }
    }
    if (y + 1 < m && a[x][y + 1] != -1) {
      if (a[x][y + 1] > a[x][y] + 1) {
        a[x][y + 1] = a[x][y] + 1;
        q.push(make_pair(x, 1 + y));
      }
    }
    if (y - 1 >= 0 && a[x][y - 1] != -1) {
      if (a[x][y - 1] > a[x][y] + 1) {
        a[x][y - 1] = a[x][y] + 1;
        q.push(make_pair(x, y - 1));
      }
    }
    q.pop();
  }
  long long ans = 0;
  for (long long i = 0, _n = (n); i < _n; i++) {
    for (long long j = 0, _n = (m); j < _n; j++) {
      if (c[i][j] != 'T' && c[i][j] != 'E' && c[i][j] != 'S') {
        if (a[x1][yyyyyy1] >= a[i][j] && c[i][j] != '0') {
          ans += (c[i][j] - 48);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
