#include <bits/stdc++.h>
using namespace std;
long long r, c;
string s[1005];
bool vis[1005][1005];
long long level[1005][1005];
long long ans_level = 200000000000000l;
long long ans = 200000000000000l;
void find(long long x, long long y);
void calc(long long x, long long y) {
  for (long long i = 0, _n = (r); i < _n; i++) {
    for (long long j = 0, _n = (c); j < _n; j++) {
      if (s[i][j] == 'T') vis[i][j] = true;
    }
  }
  ans_level = 200000000000000l;
  find(x, y);
}
void sub(long long x, long long y, queue<pair<long long, long long>> &q,
         long long &sum, long long lev) {
  if (x < 0 || x >= r)
    return;
  else if (y < 0 || y >= c)
    return;
  if (vis[x][y]) return;
  vis[x][y] = true;
  level[x][y] = lev + 1;
  q.push(make_pair(x, y));
}
void find(long long x, long long y) {
  queue<pair<long long, long long>> q;
  long long sum = 0;
  q.push(make_pair(x, y));
  vis[x][y] = true;
  while (!q.empty()) {
    auto val = q.front();
    long long lev = level[val.first][val.second];
    if (lev <= ans_level) {
      sub(val.first, val.second + 1, q, sum, lev);
      sub(val.first, val.second - 1, q, sum, lev);
      sub(val.first - 1, val.second, q, sum, lev);
      sub(val.first + 1, val.second, q, sum, lev);
      if ((s[val.first][val.second] - '0') <= 9) {
        sum += (s[val.first][val.second] - '0');
      } else if (s[val.first][val.second] == 'S') {
        ans_level = level[val.first][val.second];
      }
    }
    q.pop();
  }
  if (ans_level != 200000000000000l) {
    ans = min(ans, sum);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    cin >> r >> c;
    for (long long i = 0, _n = (r); i < _n; i++) {
      cin >> s[i];
    }
    for (long long i = 0, _n = (r); i < _n; i++) {
      for (long long j = 0, _n = (c); j < _n; j++) {
        if (s[i][j] == 'E') {
          calc(i, j);
        }
      }
    }
    cout << ans;
  }
}
