#include <bits/stdc++.h>
using namespace std;
int hm[] = {1, -1, 0, 0};
int vm[] = {0, 0, 1, -1};
int bfs(vector<vector<char> > &graph, int r, int c) {
  vector<vector<bool> > used(r, vector<bool>(c, false));
  vector<vector<int> > dist(r, vector<int>(c, 0));
  pair<int, int> p = make_pair(0, 0);
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (graph[i][j] == 'E') p = make_pair(i, j);
  queue<pair<int, int> > q;
  q.push(p);
  used[p.first][p.second] = true;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    int x0 = p.first, y0 = p.second;
    for (int k = 0; k < 4; k++) {
      int x1 = x0 + hm[k], y1 = y0 + vm[k];
      if (x1 < 0 or x1 >= r or y1 < 0 or y1 >= c) continue;
      if (used[x1][y1]) continue;
      if (graph[x1][y1] == 'T') continue;
      dist[x1][y1] = dist[x0][y0] + 1;
      used[x1][y1] = true;
      q.push(make_pair(x1, y1));
    }
  }
  pair<int, int> player = make_pair(0, 0);
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (graph[i][j] == 'S') player = make_pair(i, j);
  int count = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (dist[i][j] != 0 and
          dist[i][j] <= dist[player.first][player.second] and
          graph[i][j] != 'S')
        count += (int(graph[i][j] - '0'));
  return count;
}
int main() {
  ios::sync_with_stdio(false);
  int r = 0, c = 0;
  cin >> r >> c;
  vector<vector<char> > graph(r, vector<char>(c, '\0'));
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) cin >> graph[i][j];
  int count = bfs(graph, r, c);
  cout << count << endl;
  return 0;
}
