#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n, p, m, tmp, ans, t, start, finish;
char a[N][N];
int b[N][N];
int h[N * N];
vector<int> nei[N * N];
bool mark[N * N];
queue<int> q;
void read_input() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      b[i][j] = p;
      if (a[i][j] == 'S') start = p;
      if (a[i][j] == 'E') finish = p;
      p++;
    }
  }
  for (int i = 0; i <= n + 1; i++) {
    a[i][0] = 'T';
  }
  for (int i = 0; i <= m + 1; i++) {
    a[0][i] = 'T';
  }
  for (int i = 0; i <= n + 1; i++) {
    a[i][m + 1] = 'T';
  }
  for (int i = 0; i <= m + 1; i++) {
    a[n + 1][i] = 'T';
  }
}
void build() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] != 'T' && a[i - 1][j] != 'T') {
        nei[b[i][j]].push_back(b[i - 1][j]);
      }
      if (a[i][j] != 'T' && a[i + 1][j] != 'T') {
        nei[b[i][j]].push_back(b[i + 1][j]);
      }
      if (a[i][j] != 'T' && a[i][j - 1] != 'T') {
        nei[b[i][j]].push_back(b[i][j - 1]);
      }
      if (a[i][j] != 'T' && a[i][j + 1] != 'T') {
        nei[b[i][j]].push_back(b[i][j + 1]);
      }
    }
  }
}
void bfs(int v) {
  for (int u : nei[v]) {
    if (!mark[u]) {
      q.push(u);
      mark[u] = true;
      h[u] = h[v] + 1;
    }
  }
}
void BFS(int v) {
  mark[v] = true;
  h[v] = 0;
  q.push(v);
  while (!q.empty()) {
    bfs(q.front());
    q.pop();
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  read_input();
  build();
  BFS(finish);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] != 'S' && a[i][j] != 'T' && a[i][j] != 'E' &&
          h[b[i][j]] <= h[start] && h[b[i][j]] != 0) {
        tmp = a[i][j] - '0';
        ans += tmp;
      }
    }
  }
  cout << ans;
  return 0;
}
