#include <bits/stdc++.h>
using namespace std;
char graph[1005][1005];
int r, c;
int exit_row, exit_column, start_row, start_column;
int steps[1005][1005];
void BFS(int row, int column) {
  queue<int> row_line;
  queue<int> column_line;
  queue<int> step_taken;
  row_line.push(row);
  column_line.push(column);
  step_taken.push(0);
  steps[row][column] = 0;
  while (row_line.empty() != true) {
    int current_row = row_line.front();
    int current_column = column_line.front();
    int current_step = step_taken.front();
    row_line.pop();
    column_line.pop();
    step_taken.pop();
    if (current_row - 1 >= 0 && steps[current_row - 1][current_column] == -1 &&
        graph[current_row - 1][current_column] != 'T') {
      steps[current_row - 1][current_column] = current_step + 1;
      step_taken.push(current_step + 1);
      row_line.push(current_row - 1);
      column_line.push(current_column);
    }
    if (current_row + 1 < r && steps[current_row + 1][current_column] == -1 &&
        graph[current_row + 1][current_column] != 'T') {
      steps[current_row + 1][current_column] = current_step + 1;
      step_taken.push(current_step + 1);
      row_line.push(current_row + 1);
      column_line.push(current_column);
    }
    if (current_column - 1 >= 0 &&
        steps[current_row][current_column - 1] == -1 &&
        graph[current_row][current_column - 1] != 'T') {
      steps[current_row][current_column - 1] = current_step + 1;
      step_taken.push(current_step + 1);
      row_line.push(current_row);
      column_line.push(current_column - 1);
    }
    if (current_column + 1 < c &&
        steps[current_row][current_column + 1] == -1 &&
        graph[current_row][current_column + 1] != 'T') {
      steps[current_row][current_column + 1] = current_step + 1;
      step_taken.push(current_step + 1);
      row_line.push(current_row);
      column_line.push(current_column + 1);
    }
  }
}
int main() {
  scanf("%d %d", &r, &c);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> graph[i][j];
      if (graph[i][j] == 'S') {
        start_row = i;
        start_column = j;
      }
      if (graph[i][j] == 'E') {
        exit_row = i;
        exit_column = j;
      }
      steps[i][j] = -1;
    }
  }
  BFS(exit_row, exit_column);
  int ans = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (graph[i][j] >= '1' && graph[i][j] <= '9' &&
          steps[i][j] <= steps[start_row][start_column] && steps[i][j] != -1) {
        ans += (int)(graph[i][j] - '0');
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
