#include <bits/stdc++.h>
using namespace std;
char ch[1005][1005];
int main() {
  ios::sync_with_stdio(false);
  int r, c;
  cin >> r >> c;
  vector<complex<int> > q1, q2;
  for (int i = 1; i <= r; ++i) {
    for (int j = 1; j <= c; ++j) {
      cin >> ch[i][j];
      if ('E' == ch[i][j]) q1.push_back(complex<int>(i, j));
    }
  }
  for (int i = 1; i <= r; ++i) ch[i][0] = ch[i][c + 1] = 'T';
  for (int j = 1; j <= c; ++j) ch[0][j] = ch[r + 1][j] = 'T';
  bool flag = 0;
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  int sum = 0;
  while (!flag) {
    for (int i = 0; i != q1.size(); ++i) {
      complex<int> p = q1[i];
      int x = p.real();
      int y = p.imag();
      for (int k = 0; k != 4; ++k) {
        int xx = x + dx[k];
        int yy = y + dy[k];
        if ('T' != ch[xx][yy]) {
          q2.push_back(complex<int>(xx, yy));
          if ('S' == ch[xx][yy])
            flag = 1;
          else {
            sum += ch[xx][yy] - '0';
          }
          ch[xx][yy] = 'T';
        }
      }
      ch[x][y] = 'T';
    }
    q1.erase(q1.begin(), q1.end());
    q1.swap(q2);
  }
  cout << sum << endl;
}
