#include <bits/stdc++.h>
using namespace std;
const int maxN = 1000 + 5, oo = 1 << 30, dx[4] = {-1, 0, 0, 1},
          dy[4] = {0, -1, 1, 0};
queue<pair<int, int> > que;
char grid[maxN][maxN];
int dis[maxN][maxN];
int main() {
  int r, c, res = 0;
  pair<int, int> st, en;
  scanf("%d%d", &r, &c);
  for (int i = 0; i < r; i++) {
    scanf("%s", grid[i]);
    for (int j = 0; j < c; dis[i][j++] = oo)
      if (grid[i][j] == 'S')
        st = pair<int, int>(i, j);
      else if (grid[i][j] == 'E')
        en = pair<int, int>(i, j);
  }
  dis[en.first][en.second] = 0;
  que.push(en);
  while (!que.empty()) {
    int curx = que.front().first, cury = que.front().second, tx, ty;
    que.pop();
    for (int d = 0; d < 4; d++) {
      tx = curx + dx[d];
      ty = cury + dy[d];
      if ((grid[tx][ty] == 'S' || grid[tx][ty] >= '0' && grid[tx][ty] <= '9') &&
          dis[tx][ty] == oo) {
        dis[tx][ty] = dis[curx][cury] + 1;
        que.push(pair<int, int>(tx, ty));
      }
    }
  }
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (grid[i][j] >= '0' && grid[i][j] <= '9' &&
          dis[i][j] <= dis[st.first][st.second])
        res += (grid[i][j]) - '0';
  return printf("%d\n", res), 0;
}
