#include <bits/stdc++.h>
using namespace std;
const int max_r = 1001, max_c = 1001;
int r, c;
char graph[max_r][max_c];
int dist[max_r][max_c];
queue<pair<int, int> > q;
bool check(int i, int j) {
  return (i < r) && (j < c) && (i >= 0) && (j >= 0) && graph[i][j] != 'T' &&
         dist[i][j] == 1e9;
}
void bfs(int src_r, int src_c) {
  q.push(make_pair(src_r, src_c));
  dist[src_r][src_c] = 0;
  while (q.size()) {
    auto u = q.front();
    q.pop();
    int i = u.first, j = u.second;
    if (check(i + 1, j))
      dist[i + 1][j] = dist[i][j] + 1, q.push(make_pair(i + 1, j));
    if (check(i - 1, j))
      dist[i - 1][j] = dist[i][j] + 1, q.push(make_pair(i - 1, j));
    if (check(i, j + 1))
      dist[i][j + 1] = dist[i][j] + 1, q.push(make_pair(i, j + 1));
    if (check(i, j - 1))
      dist[i][j - 1] = dist[i][j] + 1, q.push(make_pair(i, j - 1));
  }
}
void init() {
  for (int i = 0; i < max_r; i++)
    for (int j = 0; j < max_c; j++) dist[i][j] = 1e9;
}
int main() {
  ios::sync_with_stdio(false);
  init();
  cin >> r >> c;
  int s_r, s_c, e_r, e_c;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      cin >> graph[i][j];
      if (graph[i][j] == 'S')
        s_r = i, s_c = j;
      else if (graph[i][j] == 'E')
        e_r = i, e_c = j;
    }
  bfs(e_r, e_c);
  int distance = dist[s_r][s_c];
  int cnt = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      if (dist[i][j] <= distance) {
        if (graph[i][j] <= '9' && graph[i][j] >= '0') cnt += graph[i][j] - '0';
      }
    }
  cout << cnt << endl;
  return 0;
}
