#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  int t = 1;
  while (t--) solve();
  return 0;
}
string s[1010];
int d[1010][1010];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m;
bool correct(const pair<int, int>& a) {
  return a.first >= 0 && a.first < n && a.second >= 0 && a.second < m &&
         s[a.first][a.second] != 'T';
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> s[i];
  queue<pair<int, int> > q;
  memset(d, -1, sizeof d);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'E') {
        q.push(pair<int, int>(i, j));
        d[i][j] = 0;
        break;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (int dd = 0; dd < 4; ++dd) {
      pair<int, int> nex = make_pair(cur.first + dx[dd], cur.second + dy[dd]);
      if (correct(nex) && d[nex.first][nex.second] == -1) {
        d[nex.first][nex.second] = d[cur.first][cur.second] + 1;
        q.push(nex);
      }
    }
  }
  long long ans = 0;
  int myDist = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') {
        myDist = d[i][j];
        break;
      }
    }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (s[i][j] != 'S' && s[i][j] != 'E' && d[i][j] != -1 &&
          d[i][j] <= myDist) {
        ans += (s[i][j] - '0');
      }
    }
  cout << ans;
}
