#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m;
char G[1010][1010];
pair<int, int> par;
bool vis[1010][1010];
int bfs(int x, int y) {
  queue<pair<int, int> > Q;
  Q.push(pair<int, int>(x, y));
  vis[x][y] = 1;
  int ans = 0;
  bool ok = 0;
  while (!ok) {
    int aux = (int)Q.size();
    for (int i = 0; i < aux; ++i) {
      pair<int, int> cur = Q.front();
      Q.pop();
      for (int j = 0; j < 4; ++j) {
        x = cur.first + dx[j];
        y = cur.second + dy[j];
        if ((x >= 0 && x < n && y >= 0 && y < m && G[x][y] != 'T') &&
            !vis[x][y]) {
          if (G[x][y] != 'S')
            ans += G[x][y] - '0';
          else
            ok = 1;
          vis[x][y] = 1;
          Q.push(pair<int, int>(x, y));
        }
      }
    }
  }
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> G[i][j];
      if (G[i][j] == 'E') par = pair<int, int>(i, j);
    }
  cout << bfs(par.first, par.second) << endl;
  return 0;
}
