#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b);
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve() {
  long long xx[4] = {1, 0, -1, 0};
  long long yy[4] = {0, 1, 0, -1};
  long long n, m;
  cin >> n >> m;
  char a[n + 40][m + 40];
  long long sx, sy, ex, ey;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
        a[i][j] = '0';
      }
      if (a[i][j] == 'E') {
        ex = i;
        ey = j;
        a[i][j] = '0';
      }
    }
  }
  long long dis[n + 40][m + 40];
  memset(dis, 0, sizeof dis);
  queue<pair<long long, long long> > q;
  q.push(make_pair(ex, ey));
  dis[ex][ey] = 1;
  while (!q.empty()) {
    long long first = q.front().first;
    long long second = q.front().second;
    q.pop();
    for (long long i = 0; i < 4; i++) {
      if (isdigit(a[first + xx[i]][second + yy[i]]) &&
          !dis[first + xx[i]][second + yy[i]]) {
        q.push(make_pair(xx[i] + first, yy[i] + second));
        dis[first + xx[i]][second + yy[i]] = dis[first][second] + 1;
      }
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (dis[i][j] <= dis[sx][sy] && dis[i][j] > 0) ans += a[i][j] - '0';
    }
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
