#include <bits/stdc++.h>
using namespace std;
string arr[1000 + 20];
int vis[1000 + 20][1000 + 20];
int EI, EJ, SI, SJ;
int a, b;
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
void BFS() {
  memset(vis, -1, sizeof vis);
  queue<pair<int, pair<int, int> > > Q;
  Q.push(make_pair(0, make_pair(EI, EJ)));
  vis[EI][EJ] = 0;
  while (Q.size()) {
    int C = Q.front().first, I = Q.front().second.first,
        J = Q.front().second.second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int II = I + di[i], JJ = J + dj[i];
      if (II < 0 || II >= a || JJ < 0 || JJ >= b || vis[II][JJ] != -1 ||
          arr[II][JJ] == 'T' || arr[II][JJ] == 'E')
        continue;
      Q.push(make_pair(C + 1, make_pair(II, JJ)));
      vis[II][JJ] = C + 1;
    }
  }
}
int main() {
  cin >> a >> b;
  for (int i = 0; i < a; i++) cin >> arr[i];
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++)
      if (arr[i][j] == 'E') {
        EI = i, EJ = j;
      } else if (arr[i][j] == 'S') {
        SI = i, SJ = j;
      }
  BFS();
  int cnt = 0;
  int upper = vis[SI][SJ];
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++)
      if ((vis[i][j] != -1 && vis[i][j] <= upper) &&
          (arr[i][j] != 'S' && arr[i][j] != 'E'))
        cnt += arr[i][j] - '0';
  cout << cnt << endl;
}
