#include <bits/stdc++.h>
using namespace std;
const int M = 1010;
int R, n, m, a, b, A, B, L, D[M][M], G[4] = {+0, +0, +1, -1},
                                     H[4] = {+1, -1, +0, +0};
string s[M];
bool V[M][M];
bool C(int i, int j) {
  if (i < 0 || j < 0 || n <= i || m <= j) return 0;
  return (!V[i][j] && s[i][j] != 'T');
}
void F(int i, int j) {
  vector<pair<int, int> > v;
  D[i][j] = 0, V[i][j] = 1, v.push_back(make_pair(i, j));
  for (int k = 0; k < v.size(); k++) {
    i = v[k].first;
    j = v[k].second;
    for (int l = 0; l < 4; l++)
      if (C(i + G[l], j + H[l]))
        D[i + G[l]][j + H[l]] = D[i][j] + 1, V[i + G[l]][j + H[l]] = 1,
                        v.push_back(make_pair(i + G[l], j + H[l]));
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S')
        a = i, b = j;
      else if (s[i][j] == 'E')
        A = i, B = j;
  }
  F(a, b);
  L = D[A][B];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) V[i][j] = 0;
  F(A, B);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (D[i][j] <= L && '0' <= s[i][j] && s[i][j] <= '9' && V[i][j])
        R += s[i][j] - '0';
  cout << R << endl;
  return 0;
}
