#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
int x[] = {1, 0, -1, 0};
int y[] = {0, 1, 0, -1};
int r, c;
vector<vector<int>> mas;
vector<vector<int>> dist;
bool used[maxn][maxn];
pair<int, int> st, e;
int main() {
  scanf("%d%d", &r, &c);
  for (int i = 0; i <= r + 3; i++)
    for (int j = 0; j <= c + 3; j++) used[i][j] = true;
  mas = vector<vector<int>>(r + 2, vector<int>(c + 2));
  dist = vector<vector<int>>(r + 2, vector<int>(c + 2, 1e+9));
  for (int i = 1; i <= r; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < ((int)s.length()); j++) {
      if (s[j] == 'T') {
        mas[i][j + 1] = -1;
        continue;
      }
      used[i][j + 1] = false;
      if (s[j] == 'S') {
        st = make_pair(i, j + 1);
        continue;
      }
      if (s[j] == 'E') {
        e = make_pair(i, j + 1);
        continue;
      }
      mas[i][j + 1] = s[j] - '0';
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(e.first, e.second));
  used[e.first][e.second] = true;
  dist[e.first][e.second] = 0;
  while (!q.empty()) {
    pair<int, int> from = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int toi = from.first + x[i];
      int toj = from.second + y[i];
      if (!used[toi][toj]) {
        used[toi][toj] = true;
        dist[toi][toj] = dist[from.first][from.second] + 1;
        q.push(make_pair(toi, toj));
      }
    }
  }
  int answer = 0;
  int val = dist[st.first][st.second];
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      if (mas[i][j] != -1) {
        if (dist[i][j] <= val) answer += mas[i][j];
      }
    }
  cout << answer;
  return 0;
}
