#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[1023][1023] = {0};
int dist[1023][1023] = {0};
int dist2[123][123] = {0};
int cnt = 0;
int sd = INT_MAX;
queue<pair<int, int> > q;
bool isValid(int i, int j) {
  if (i >= n || j >= m || i < 0 || j < 0) return 0;
  if (A[i][j] == -3) return 0;
  return 1;
}
void bfs(int i, int j, int d) {
  dist[i][j] = d;
  q.push(make_pair(i, j));
  while (!q.empty()) {
    i = q.front().first;
    j = q.front().second;
    if (isValid(i + 1, j)) {
      if (dist[i + 1][j] > dist[i][j] + 1) {
        dist[i + 1][j] = dist[i][j] + 1;
        q.push(make_pair(i + 1, j));
      }
    }
    if (isValid(i - 1, j)) {
      if (dist[i - 1][j] > dist[i][j] + 1) {
        dist[i - 1][j] = dist[i][j] + 1;
        q.push(make_pair(i - 1, j));
      }
    }
    if (isValid(i, j + 1)) {
      if (dist[i][j + 1] > dist[i][j] + 1) {
        dist[i][j + 1] = dist[i][j] + 1;
        q.push(make_pair(i, j + 1));
      }
    }
    if (isValid(i, j - 1)) {
      if (dist[i][j - 1] > dist[i][j] + 1) {
        dist[i][j - 1] = dist[i][j] + 1;
        q.push(make_pair(i, j - 1));
      }
    }
    q.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  pair<int, int> s, e;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char x;
      cin >> x;
      if (x == 'S') {
        A[i][j] = -1;
        s = make_pair(i, j);
      } else if (x == 'E') {
        A[i][j] = -2;
        e = make_pair(i, j);
      } else if (x == 'T')
        A[i][j] = -3;
      else
        A[i][j] = x - '0';
      dist[i][j] = INT_MAX;
    }
  }
  bfs(e.first, e.second, 0);
  int dsource = dist[s.first][s.second];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (A[i][j] > 0) {
        if (dist[i][j] <= dist[s.first][s.second]) {
          ans += A[i][j];
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
