#include <bits/stdc++.h>
using namespace std;
const long long dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
const long long dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
long long r, c;
char a[1005][1005];
long long start_x, end_x, start_y, end_y;
long long dist[1005][1005];
struct point {
  long long x, y;
};
void bfs() {
  memset(dist, -1, sizeof dist);
  queue<point> q;
  point p;
  p.x = end_x;
  p.y = end_y;
  q.push(p);
  dist[end_x][end_y] = 0;
  while (q.size()) {
    point tp = q.front();
    q.pop();
    for (long long i = 1; i < 8; i += 2) {
      long long new_x = tp.x + dx[i];
      long long new_y = tp.y + dy[i];
      point new_point;
      new_point.x = new_x;
      new_point.y = new_y;
      if (new_y >= 0 and new_y < c and new_x >= 0 and new_x < r and
          a[new_x][new_y] >= '0' and a[new_x][new_y] <= '9' and
          dist[new_x][new_y] == -1) {
        q.push(new_point);
        dist[new_point.x][new_point.y] = dist[tp.x][tp.y] + 1;
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long test = 1;
  while (test--) {
    cin >> r >> c;
    for (long long i = 0; i < r; i++)
      for (long long j = 0; j < c; j++) {
        cin >> a[i][j];
        switch (a[i][j]) {
          case 'S':
            start_x = i;
            start_y = j;
            break;
          case 'E':
            end_x = i;
            end_y = j;
            break;
        }
      }
    a[start_x][start_y] = '0';
    bfs();
    long long mn = dist[start_x][start_y];
    long long ans = 0;
    for (long long i = 0; i < r; i++) {
      for (long long j = 0; j < c; j++) {
        if (a[i][j] >= '0' and a[i][j] <= '9' and dist[i][j] <= mn and
            dist[i][j] != -1)
          ans += a[i][j] - '0';
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
