#include <bits/stdc++.h>
using namespace std;
int MAX = 1000000;
int MIN = -1000000;
int INF = 1000000000;
int x4[4] = {0, 1, 0, -1};
int y4[4] = {1, 0, -1, 0};
int x8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int y8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int i, j, k;
int jarak[1010][1010];
bool udah[1010][1010];
int main() {
  int r, c;
  string str[1010];
  for (i = (0); i < (1005); i++)
    for (j = (0); j < (1005); j++) jarak[i][j] = 1000000000;
  cin >> r >> c;
  for (i = (0); i < (r); i++) cin >> str[i];
  int X, Y;
  for (i = (0); i < (r); i++) {
    for (j = (0); j < (c); j++) {
      if (str[i][j] == 'E') {
        X = i;
        Y = j;
      }
    }
  }
  memset((udah), (false), sizeof(udah));
  ;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(X, Y));
  int pan;
  int jar = 0;
  while (q.empty() == false) {
    pan = q.size();
    for (i = (0); i < (pan); i++) {
      pair<int, int> temp = q.front();
      q.pop();
      int x = temp.first;
      int y = temp.second;
      udah[x][y] = true;
      jarak[x][y] = jar;
      for (j = (0); j < (4); j++) {
        if (x + x4[j] >= 0 && x + x4[j] < r && y + y4[j] >= 0 &&
            y + y4[j] < c) {
          if (udah[x + x4[j]][y + y4[j]] == false &&
              str[x + x4[j]][y + y4[j]] != 'T') {
            q.push(pair<int, int>(x + x4[j], y + y4[j]));
            udah[x + x4[j]][y + y4[j]] = true;
          }
        }
      }
    }
    jar++;
  }
  int pemain;
  for (i = (0); i < (r); i++) {
    for (j = (0); j < (c); j++) {
      if (str[i][j] == 'S') pemain = jarak[i][j];
    }
  }
  int ans = 0;
  for (i = (0); i < (r); i++) {
    for (j = (0); j < (c); j++) {
      if (str[i][j] != 'T' && str[i][j] != 'S' && str[i][j] != 'E') {
        if (jarak[i][j] <= pemain) ans += (str[i][j] - '0');
      }
    }
  }
  cout << ans << endl;
  {
    fflush(stdin);
    getchar();
  }
  return 0;
}
