#include <iostream>
#include <queue>
#include <vector>
///2004-2022
///2108(50)
///2115(90)-rc
///2120(100)-did not read problem description completely;
using namespace std;
int main() {
int r,c;
int dir[4][2]= {1,0,-1,0,0,1,0,-1};
cin>>r>>c;
char sco[r][c];
vector<vector<int> >vst(r,vector<int>(c,0));
vector<vector<int> >vst_b(r,vector<int>(c,0));
int s_x,s_y;
int t_x,t_y;
for (int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
cin>>sco[i][j];
if (sco[i][j]=='S') {
s_x=i;
s_y=j;
}
if (sco[i][j]=='T') {
t_x=i;
t_y=j;
}
}
}
queue<int >qux;
queue<int >quy;
vst[s_x][s_y]=1;
qux.push(s_x);
quy.push(s_y);
int step=1;
while(!qux.empty()) {
int x=qux.front();
qux.pop();
int y=quy.front();
quy.pop();
step++;
if (sco[x][y]=='|') {
for (int i=0; i<2; i++) {
int t_x=x+dir[i][0];
int t_y=y+dir[i][1];
if (-1<t_x&&t_x<r) {
if (-1<t_y&&t_y<c) {
if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
vst[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
}
}
}
} else if (sco[x][y]=='-') {
for (int i=2; i<4; i++) {
int t_x=x+dir[i][0];
int t_y=y+dir[i][1];
if (-1<t_x&&t_x<r) {
if (-1<t_y&&t_y<c) {
if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
vst[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
}
}
}
} else if (sco[x][y]=='.') {
for (int i=0; i<1; i++) {
int t_x=x+dir[i][0];
int t_y=y+dir[i][1];
if (-1<t_x&&t_x<r) {
if (-1<t_y&&t_y<c) {
if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
vst[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
}
}
}
} else if (sco[x][y]!='#') {
for (int i=0; i<4; i++) {
int t_x=x+dir[i][0];
int t_y=y+dir[i][1];
if (-1<t_x&&t_x<r) {
if (-1<t_y&&t_y<c) {
if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
vst[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
}
}
}
}
}
if (vst[t_x][t_y]==0)
{
cout<<"I'm stuck!"<<endl;
return 0;
}
// for (int i=0; i<r; i++) {
// for (int j=0; j<c; j++) {
// cout<<vst[i][j]<<" ";
// }
// cout<<endl;
// }
vst_b[t_x][t_y]=1;
qux.push(t_x);
quy.push(t_y);
step=1;
while(!qux.empty()) {
int x=qux.front();
qux.pop();
int y=quy.front();
quy.pop();
step++;
for (int i=0; i<4; i++) {
int t_x=x+dir[i][0];
int t_y=y+dir[i][1];
if (-1<t_x&&t_x<r &&-1<t_y&&t_y<c) {
if (!vst_b[t_x][t_y]&&sco[t_x][t_y]!='#') {
if (sco[t_x][t_y]=='|') {
if (i==0||i==1) {
vst_b[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
} else if (sco[t_x][t_y]=='-') {
if (i==2||i==3) {
vst_b[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
} else if (sco[t_x][t_y]=='.') {
if (i==1) {
vst_b[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
} else {
vst_b[t_x][t_y]=step;
qux.push(t_x);
quy.push(t_y);
}
}
}
}
}
int cnt=0;
for (int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
//cout<<vst_b[i][j]<<" ";
if (vst[i][j]&&(!vst_b[i][j])) {
cnt++;
}
}
// cout<<endl;
}
cout << cnt << endl;
return 0;
}