#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, d;
};
vector<pair<int, int> > v;
char field[1005][1005];
int dis[1005][1005];
int ds = 0;
void bfs(int sx, int sy, int n, int m) {
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  ds = 1e9;
  fill(dis[0], dis[0] + 1005 * 1005, 1e9);
  queue<node> que;
  que.push((node){sx, sy, 0});
  field[sx][sy] = 'T';
  while (!que.empty()) {
    node t = que.front();
    que.pop();
    field[t.x][t.y] = 'T';
    bool find = false;
    for (int i = 0; i < 4; i++) {
      int xx = t.x + dx[i];
      int yy = t.y + dy[i];
      int dd = t.d + 1;
      if (0 <= xx && xx < n && 0 <= yy && yy < m) {
        if (isdigit(field[xx][yy]) && field[xx][yy] != '0')
          v.push_back(make_pair(field[xx][yy] - '0', dd));
        else if (field[xx][yy] == 'S')
          ds = dd;
        if (field[xx][yy] != 'T') {
          field[xx][yy] = 'T';
          que.push((node){xx, yy, dd});
        }
      }
    }
  }
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 0; i < n; i++) scanf("%s", field[i]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (field[i][j] == 'E') bfs(i, j, n, m);
      }
    }
    int ans = 0;
    for (int i = 0; i < v.size(); i++)
      if (v[i].second <= ds) ans += v[i].first;
    v.clear();
    printf("%d\n", ans);
  }
}
