#include <bits/stdc++.h>
using namespace std;
char t[1000][1000];
unsigned int sr, sc, er, ec;
unsigned int m[1000][1000];
bool f[1000][1000];
int main(int argc, char* argv[]) {
  unsigned int r, c;
  cin >> r >> c;
  for (unsigned int i = 0; i < r; ++i) {
    for (unsigned int j = 0; j < c; ++j) {
      cin >> t[i][j];
      if (t[i][j] == 'S') {
        sr = i;
        sc = j;
      } else if (t[i][j] == 'E') {
        er = i;
        ec = j;
      } else if (t[i][j] == 'T')
        f[i][j] = true;
    }
  }
  deque<pair<unsigned int, unsigned int>> v;
  pair<unsigned int, unsigned int> z(-1, -1);
  v.push_back(make_pair(er, ec));
  v.push_back(z);
  f[er][ec] = true;
  unsigned int tm = 1;
  while (!v.empty()) {
    pair<unsigned int, unsigned int> cur = v.front();
    v.pop_front();
    if (!v.empty()) {
      if (cur == z) {
        ++tm;
        cur = v.front();
        v.pop_front();
        v.push_back(z);
      }
      if (cur.first > 0) {
        --cur.first;
        if (!f[cur.first][cur.second]) {
          v.push_back(cur);
          f[cur.first][cur.second] = true;
          m[cur.first][cur.second] = tm;
        }
        ++cur.first;
      }
      if (cur.first < r - 1) {
        ++cur.first;
        if (!f[cur.first][cur.second]) {
          v.push_back(cur);
          f[cur.first][cur.second] = true;
          m[cur.first][cur.second] = tm;
        }
        --cur.first;
      }
      if (cur.second > 0) {
        --cur.second;
        if (!f[cur.first][cur.second]) {
          v.push_back(cur);
          f[cur.first][cur.second] = true;
          m[cur.first][cur.second] = tm;
        }
        ++cur.second;
      }
      if (cur.second < c - 1) {
        ++cur.second;
        if (!f[cur.first][cur.second]) {
          v.push_back(cur);
          f[cur.first][cur.second] = true;
          m[cur.first][cur.second] = tm;
        }
        --cur.second;
      }
    }
  }
  unsigned int ch = m[sr][sc];
  unsigned long long q = 0;
  for (unsigned int i = 0; i < r; ++i) {
    for (unsigned int j = 0; j < c; ++j) {
      if (t[i][j] >= '1' && t[i][j] <= '9') {
        if (m[i][j] && m[i][j] <= ch) q += t[i][j] - '0';
      }
    }
  }
  cout << q << endl;
  return 0;
}
