#include <bits/stdc++.h>   using namespace std;   unsigned long long int dist[1150][1150], visited[1150][1150];   char ans[1050][1050];   void solve() {     long long n, i, j, m;     cin >> n >> m;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) cin >> ans[i][j];     }     int startx, starty, endx, endy;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (ans[i][j] ==  E ) {           endx = i;           endy = j;         }         if (ans[i][j] ==  S ) {           startx = i;           starty = j;         }       }     }     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) {         dist[i][j] = INT_MAX;         visited[i][j] = 0;       }     int rowcol[] = {0, 0, 1, -1};     int colcol[] = {1, -1, 0, 0};     dist[endx][endy] = 0;     visited[endx][endy] = 1;     queue<pair<int, int> > q;     q.push({endx, endy});     while (q.empty() == 0) {       pair<int, int> point = q.front();       q.pop();       int x = point.first;       int y = point.second;       for (i = 0; i < 4; i++) {         int x1 = x + rowcol[i];         int y1 = y + colcol[i];         if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && ans[x1][y1] !=  T ) {           if (visited[x1][y1] == 0) {             dist[x1][y1] = dist[x][y] + 1;             visited[x1][y1] = 1;             q.push({x1, y1});           }         }       }     }     unsigned long long int count = 0;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (startx == i && starty == j) {           continue;         }         if (dist[startx][starty] >= dist[i][j] && ans[i][j] !=  0  &&             ans[i][j] !=  T  && ans[i][j] !=  E )           count += ans[i][j] -  0 ;       }     }     cout << count << endl;   }   int main() {     long long n = 1, x1, y1, x2, y2, i;     while (n--) {       solve();     }   }   