#include <bits/stdc++.h>
using namespace std;
int n, m, s, x, y, z, p, a;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
bool vis[1001][1001], vis2[1001][1001];
string arr[1001];
int BFS1(int i, int j) {
  queue<pair<pair<int, int>, int> > que;
  que.push({{i, j}, 0});
  while (que.size()) {
    pair<pair<int, int>, int> cur = que.front();
    que.pop();
    x = cur.first.first, y = cur.first.second, z = cur.second;
    if (arr[x][y] == 'E') return z;
    for (int c = 0; c < 4; c++) {
      int xx = x + dx[c], yy = y + dy[c];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
        if (arr[xx][yy] != 'T' && arr[xx][yy] != 'S' && !vis[xx][yy]) {
          vis[xx][yy] = 1;
          que.push({{xx, yy}, z + 1});
        }
      }
    }
  }
}
int BFS2(int i, int j) {
  queue<pair<pair<int, int>, int> > que;
  que.push({{i, j}, 0});
  while (que.size()) {
    pair<pair<int, int>, int> cur = que.front();
    que.pop();
    x = cur.first.first, y = cur.first.second, z = cur.second;
    if (z == p) return a;
    for (int c = 0; c < 4; c++) {
      int xx = x + dx[c], yy = y + dy[c];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
        if (arr[xx][yy] != 'T' && arr[xx][yy] != 'E' && arr[xx][yy] != 'S' &&
            !vis2[xx][yy]) {
          vis2[xx][yy] = 1;
          a += (arr[xx][yy] - '0');
          que.push({{xx, yy}, z + 1});
        }
      }
    }
  }
  return a;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (arr[i][j] == 'S') {
        p = BFS1(i, j);
        break;
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (arr[i][j] == 'E') {
        cout << BFS2(i, j);
        break;
      }
  return 0;
}
