#include <bits/stdc++.h>
using namespace std;
const int maxi = 1e6 + 6;
int a[1010][1010];
int n, m;
int xs, xe, ys, ye;
int ans;
int tim;
int ob[1010][1010];
queue<pair<pair<int, int>, int>> q;
int untura(int x, int y) {
  if (x == 0 || x > n || y == 0 || y > m)
    return 0;
  else
    return 1;
}
void bfs() {
  q.push({{xe, ye}, 1});
  int px, py;
  while (!q.empty()) {
    pair<pair<int, int>, int> nd = q.front();
    q.pop();
    int xx = nd.first.first;
    int yy = nd.first.second;
    if (!ob[xx][yy]) {
      tim = nd.second;
      ob[xx][yy] = tim;
      px = xx - 1;
      py = yy;
      if (untura(px, py) && !ob[px][py] && a[px][py] != -1)
        q.push({{px, py}, tim + 1});
      px = xx + 1;
      py = yy;
      if (untura(px, py) && !ob[px][py] && a[px][py] != -1)
        q.push({{px, py}, tim + 1});
      px = xx;
      py = yy - 1;
      if (untura(px, py) && !ob[px][py] && a[px][py] != -1)
        q.push({{px, py}, tim + 1});
      px = xx;
      py = yy + 1;
      if (untura(px, py) && !ob[px][py] && a[px][py] != -1)
        q.push({{px, py}, tim + 1});
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++)
      if (s[j] == 'S') {
        xs = i;
        ys = j + 1;
      } else if (s[j] == 'E') {
        xe = i;
        ye = j + 1;
      } else if (s[j] == 'T')
        a[i][j + 1] = -1;
      else
        a[i][j + 1] = s[j] - '0';
  }
  bfs();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ob[xs][ys] >= ob[i][j] && a[i][j] != -1 && ob[i][j]) ans += a[i][j];
  cout << ans << "\n";
  return 0;
}
