#include <bits/stdc++.h>
using namespace std;
template <class _T>
_T MAX(_T p1, _T p2) {
  return (p1 > p2) ? p1 : p2;
}
template <class _T>
_T MIN(_T p1, _T p2) {
  return (p1 < p2) ? p1 : p2;
}
template <class _Tv>
inline string join(_Tv &v, string sep = " ") {
  ostringstream s;
  for (__typeof__((v).begin()) it = (v).begin(); it != (v).end(); ++it) {
    if (it != v.begin()) s << sep;
    s << *it;
  };
  return s.str();
}
int m[1010][1010], cost[1010][1010], inq[1010][1010];
unsigned int step[1010][1010];
int sx, sy, gx, gy;
const int dir[][2] = {{1, 0}, {-1, 0}, {0, 1},  {0, -1},
                      {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
int main() {
  int h, w;
  cin >> h >> w;
  for (int y = (0); y < (0 + h); ++y) {
    string s;
    cin >> s;
    for (int x = (0); x < (0 + w); ++x) {
      if (s[x] == 'T') m[y][x] = -1;
      if (s[x] == 'S') sx = x, sy = y;
      if (s[x] == 'E') gx = x, gy = y;
      if (isdigit(s[x])) m[y][x] = s[x] - '0';
    }
  }
  deque<pair<int, int> > q;
  set<int> s;
  memset(step, -1, sizeof(step));
  step[sy][sx] = 0;
  q.push_back(make_pair(sx, sy));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    inq[y][x] = 0;
    for (int i = (0); i < (0 + 4); ++i) {
      int mx = x + dir[i][0];
      int my = y + dir[i][1];
      if (mx < 0 || my < 0 || mx >= w || my >= h) continue;
      if (m[my][mx] < 0) continue;
      if (step[my][mx] > step[y][x] + 1) {
        step[my][mx] = step[y][x] + 1;
        if (inq[my][mx] == 0) {
          q.push_back(make_pair(mx, my));
          inq[my][mx] = 1;
        }
      }
    }
  }
  unsigned int st = step[gy][gx];
  memset(step, -1, sizeof(step));
  step[gy][gx] = 0;
  int count = 0;
  q.push_back(make_pair(gx, gy));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    inq[y][x] = 0;
    for (int i = (0); i < (0 + 4); ++i) {
      int mx = x + dir[i][0];
      int my = y + dir[i][1];
      if (mx < 0 || my < 0 || mx >= w || my >= h) continue;
      if (m[my][mx] < 0) continue;
      if (step[my][mx] > step[y][x] + 1) {
        step[my][mx] = step[y][x] + 1;
        if (m[my][mx] > 0 && step[my][mx] <= st) {
          count += m[my][mx];
          m[my][mx] = 0;
        }
        if (inq[my][mx] == 0) {
          q.push_back(make_pair(mx, my));
          inq[my][mx] = 1;
        }
      }
    }
  }
  for (int y = (0); y < (0 + h); ++y) {
    for (int x = (0); x < (0 + w); ++x) {
      ;
    };
  }
  cout << count << endl;
  return 0;
}
