#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int i, j;
} Tile;
Tile newTile(int i, int j) {
  Tile out;
  out.i = i;
  out.j = j;
  return out;
}
int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<string> map(n);
  for (int i = 0; i < n; ++i) {
    string t;
    cin >> t;
    map[i] = t;
  }
  queue<Tile> q;
  vector<vector<int> > reg(n, vector<int>(m, -1));
  Tile exit;
  Tile start;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (map[i][j] == 'E') {
        Tile temp = {i, j};
        exit = temp;
      }
      if (map[i][j] == 'S') {
        Tile temp = {i, j};
        start = temp;
      }
    }
  int distp = INT_MAX;
  int n_peleas = 0;
  reg[exit.i][exit.j] = 0;
  q.push(exit);
  while (!q.empty()) {
    Tile u = q.front();
    q.pop();
    if (map[u.i][u.j] == 'S') distp = reg[u.i][u.j];
    if (reg[u.i][u.j] > distp) continue;
    if (map[u.i][u.j] != 'E' && map[u.i][u.j] != 'S')
      n_peleas += map[u.i][u.j] - '0';
    vector<Tile> Gu;
    if (u.i - 1 >= 0 && map[u.i - 1][u.j] != 'T')
      Gu.push_back(newTile(u.i - 1, u.j));
    if (u.j - 1 >= 0 && map[u.i][u.j - 1] != 'T')
      Gu.push_back(newTile(u.i, u.j - 1));
    if (u.i + 1 < n && map[u.i + 1][u.j] != 'T')
      Gu.push_back(newTile(u.i + 1, u.j));
    if (u.j + 1 < m && map[u.i][u.j + 1] != 'T')
      Gu.push_back(newTile(u.i, u.j + 1));
    for (int i = 0; i < Gu.size(); i++) {
      if (reg[Gu[i].i][Gu[i].j] == -1) {
        reg[Gu[i].i][Gu[i].j] = reg[u.i][u.j] + 1;
        q.push(Gu[i]);
      }
    }
  }
  cout << n_peleas << endl;
  return 0;
}
