#include <bits/stdc++.h>
using namespace std;
string s[1111];
int ans, b[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
bool vis[1111][1111];
void bfs(int i, int j) {
  queue<pair<int, int> > q;
  q.push(make_pair(i, j));
  vis[i][j] = 1;
  pair<int, int> a;
  bool check = 1;
  while (check) {
    int tmp = q.size();
    for (; tmp > 0; tmp--) {
      a = q.front();
      q.pop();
      for (int k = 0; k < 4; k++) {
        if (!vis[a.first + b[k][0]][a.second + b[k][1]]) {
          vis[a.first + b[k][0]][a.second + b[k][1]] = 1;
          q.push(make_pair(a.first + b[k][0], a.second + b[k][1]));
          if (s[a.first + b[k][0]][a.second + b[k][1]] == 'S')
            check = 0;
          else
            ans += s[a.first + b[k][0]][a.second + b[k][1]] - '0';
        }
      }
    }
  }
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 0; i <= m + 1; i++) {
    s[0] += 'T';
    s[n + 1] += 'T';
  }
  for (int i = 1; i <= n; i++) s[i] = 'T' + s[i] + 'T';
  for (int i = 0; i <= n + 1; i++)
    for (int j = 0; j <= m + 1; j++)
      if (s[i][j] == 'T') vis[i][j] = 1;
  for (int i = 0; i <= n + 1; i++)
    for (int j = 0; j <= m + 1; j++)
      if (s[i][j] == 'E') bfs(i, j);
  cout << ans << endl;
  return 0;
}
