#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.14159265359;
vector<long long> primes;
void GeneratingPrimes(int top) {
  vector<bool> p(top + 1, 1);
  p[0] = p[1] = 0;
  for (int i = 2; i <= sqrt(top); i++) {
    if (p[i]) {
      for (int j = i * i; j <= top; j += i) {
        p[j] = 0;
      }
    }
  }
  for (int i = 0; i < top + 1; i++)
    if (p[i]) primes.push_back(i);
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void EXTgcd(long long a, long long b, long long& x0, long long& y0) {
  if (b == 0) {
    x0 = 1, y0 = 0;
    return;
  }
  EXTgcd(b, a % b, x0, y0);
  long long x1 = y0;
  long long y1 = x0 - (a / b) * (y0);
  y1 = y1;
  x0 = x1;
  y0 = y1;
}
long long power(long long n, long long p, long long mod) {
  if (!p) return 1;
  long long ans = 1;
  while (p > 1) {
    if (p & 1) ans = (ans * n) % mod;
    n = (n * n) % mod;
    p /= 2;
  }
  return (ans * n) % mod;
}
double dist(double a, double b, double x, double y) {
  return sqrt(pow(a - x, 2) + pow(b - y, 2));
}
double dist3(double a, double b, double c, double x, double y, double z) {
  return sqrt(pow(a - x, 2) + pow(b - y, 2) + pow(c - z, 2));
}
int xadd[9] = {1, -1, 0, 1, -1, 0, 1, -1, 0};
int yadd[9] = {1, -1, 0, -1, 0, 1, 0, 1, -1};
int xx[4] = {0, 1, 0, -1};
int yy[4] = {1, 0, -1, 0};
long long const N = 1e3 + 10;
long long const INF = 1e18;
long long const mod = 1000000009;
int n, m, d[N][N], ans;
vector<string> v;
pair<int, int> st, e;
bool ok(pair<int, int> p) {
  return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < m &&
          d[p.first][p.second] == -1 && v[p.first][p.second] != 'T');
}
void bfs() {
  queue<pair<int, int> > q;
  q.push(e);
  d[e.first][e.second] = 0;
  int dd = 1;
  while (q.size()) {
    int s = q.size();
    while (s--) {
      pair<int, int> cur = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        pair<int, int> x = make_pair(cur.first + xx[i], cur.second + yy[i]);
        if (ok(x)) q.push(x), d[x.first][x.second] = dd;
      }
    }
    dd++;
  }
}
int main() {
  memset(d, -1, sizeof d);
  cin >> n >> m;
  v.resize(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      v[i].push_back(c);
      if (c == 'E')
        e = make_pair(i, j);
      else if (c == 'S')
        st = make_pair(i, j);
    }
  }
  bfs();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if ('0' <= v[i][j] && v[i][j] <= '9' && ~d[i][j] &&
          d[i][j] <= d[st.first][st.second]) {
        ans += (v[i][j] - '0');
      }
  cout << ans;
  return 0;
}
