#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
char a[N][N];
int dis[N][N], n, m;
bool vis[N][N];
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool is(int x, int y) {
  return (x >= 0 && x < n && y >= 0 && y < m && a[x][y] != 'T' && !vis[x][y]);
}
void BFS(int x, int y) {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) dis[i][j] = 1e9;
  deque<pair<int, int> > q;
  q.push_back(make_pair(x, y));
  vis[x][y] = 1, dis[x][y] = 0;
  while (!q.empty()) {
    pair<int, int> v = q.front();
    for (int i = 0; i < 4; ++i) {
      pair<int, int> u = make_pair(v.first + d[i][0], v.second + d[i][1]);
      if (is(u.first, u.second))
        vis[u.first][u.second] = 1,
        dis[u.first][u.second] = dis[v.first][v.second] + 1, q.push_back(u);
    }
    q.pop_front();
  }
}
int main() {
  ios::sync_with_stdio(0);
  int x, y, xx, yy;
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'E') x = i, y = j;
      if (a[i][j] == 'S') xx = i, yy = j;
    }
  BFS(x, y);
  int tot = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] != 'E' && a[i][j] != 'S' && a[i][j] != 'T' &&
          a[i][j] != '0' && dis[i][j] <= dis[xx][yy])
        tot += a[i][j] - '0';
    }
  }
  cout << tot << '\n';
}
