#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double EPS = 1e-9;
int mods(int a, int b) { return (b + (a % b)) % b; }
int ex, ey, r, c, battles, optimal, newR, newC, dist[1005][1005];
char grid[1005][1005];
vector<int> enemies;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'E') {
        ex = i;
        ey = j;
      }
    }
  }
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) dist[i][j] = INF;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  dist[ex][ey] = 0;
  pq.push(pair<int, pair<int, int>>(0, pair<int, int>(ex, ey)));
  while (!pq.empty()) {
    pair<int, pair<int, int>> front = pq.top();
    pq.pop();
    int d = front.first, x = front.second.first, y = front.second.second;
    if (d > dist[x][y]) continue;
    for (int i = 0; i < 4; i++) {
      newR = x + dr[i], newC = y + dc[i];
      if (newR < 0 || newR >= r || newC < 0 || newC >= c ||
          grid[newR][newC] == 'T')
        continue;
      if (dist[x][y] + 1 < dist[newR][newC]) {
        dist[newR][newC] = dist[x][y] + 1;
        char ch = grid[newR][newC];
        if (ch == 'S')
          optimal = dist[newR][newC];
        else if (isdigit(ch) && ch > '0') {
          int cnt = ch - '0';
          for (int j = 0; j < cnt; j++) enemies.push_back(dist[newR][newC]);
        }
        pq.push(pair<int, pair<int, int>>(dist[newR][newC],
                                          pair<int, int>(newR, newC)));
      }
    }
  }
  for (int i = 0; i < enemies.size(); i++)
    if (enemies[i] <= optimal) battles++;
  cout << battles << '\n';
  return 0;
}
