#include <bits/stdc++.h>
using namespace std;
long long n, m, nilai[1005][1005];
char arr[1005][1005];
queue<pair<long long, long long>> q;
bool vis[1005][1005];
pair<long long, long long> start;
long long row[] = {1, 0, -1, 0};
long long col[] = {0, 1, 0, -1};
bool memenuhi(long long x, long long y) {
  return (x > 0 && y > 0 && x <= n && y <= m && !vis[x][y] && arr[x][y] != 'T');
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      nilai[i][j] = 1e9 + 7;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> arr[i][j];
      if (arr[i][j] == 'E') {
        nilai[i][j] = 0;
        q.push({i, j});
        vis[i][j] = 1;
      }
      if (arr[i][j] == 'S') start = {i, j};
    }
  }
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int baris = row[k] + x;
      int kolom = col[k] + y;
      if (memenuhi(baris, kolom)) {
        nilai[baris][kolom] = nilai[x][y] + 1;
        vis[baris][kolom] = 1;
        q.push({baris, kolom});
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (arr[i][j] >= '1' && arr[i][j] <= '9' &&
          nilai[i][j] <= nilai[start.first][start.second]) {
        int angka = arr[i][j] - '0';
        ans += angka;
      }
    }
  }
  cout << ans << '\n';
}
