#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m;
int inf = 1e9;
vector<string> arr;
vector<vector<int> > vis;
vector<vector<int> > dis;
bool check(int x, int y) {
  if (x < 0 or y < 0 or x >= n or y >= m) {
    return false;
  }
  if (vis[x][y] == 1 or arr[x][y] == 'T') {
    return false;
  }
  return true;
}
void bfs(int x, int y) {
  vis[x][y] = 1;
  dis[x][y] = 0;
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int a = p.first, b = p.second;
    for (int i = 0; i < 4; i++) {
      if (check(a + dx[i], b + dy[i])) {
        vis[a + dx[i]][b + dy[i]] = 1;
        dis[a + dx[i]][b + dy[i]] = 1 + dis[a][b];
        q.push(make_pair(a + dx[i], b + dy[i]));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  arr.resize(n);
  vis.resize(n, vector<int>(m, 0));
  dis.resize(n, vector<int>(m, inf));
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int x, y;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == 'E') {
        bfs(i, j);
      } else if (arr[i][j] == 'S') {
        x = i, y = j;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] != 'E' and arr[i][j] != 'S' and arr[i][j] != 'T') {
        if (dis[i][j] <= dis[x][y]) {
          ans += (int)(arr[i][j] - '0');
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
