#include <bits/stdc++.h>
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define ull unsigned ll
#define pll pair<ll,ll>
#define ll long long
#define INF 1e18
#define mod 998244353
#define maxn 1010
#define logN 17
using namespace std;

ll n,m;
ll mat[maxn][maxn];
ll dist1[maxn][maxn];
ll dist2[maxn][maxn];
vector<pll> va;
pll st;

void bfs1(){
    queue<pll> red;
    red.push(st);
    dist1[st.fi][st.se]=1;
    while(!red.empty()){
        pll poomm=red.front();
        red.pop();
        ll tx=poomm.fi,ty=poomm.se;
        ll dx=1,dy=0;
        ll xx=tx+dx; ll yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist1[xx][yy]==0){
            dist1[xx][yy]=dist1[tx][ty]+1;
            red.push({xx,yy});
        }
        dx=-1,dy=0;
        xx=tx+dx; yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist1[xx][yy]==0){
            dist1[xx][yy]=dist1[tx][ty]+1;
            red.push({xx,yy});
        }
        dx=0,dy=1;
        xx=tx+dx; yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist1[xx][yy]==0){
            dist1[xx][yy]=dist1[tx][ty]+1;
            red.push({xx,yy});
        }
        dx=0,dy=-1;
        xx=tx+dx; yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist1[xx][yy]==0){
            dist1[xx][yy]=dist1[tx][ty]+1;
            red.push({xx,yy});
        }
    }
}

void bfs2(){
    queue<pll> red;
    for(ll i=0;i<va.size();i++){
        red.push(va[i]);
        dist2[va[i].fi][va[i].se]=1;
    }
    while(!red.empty()){
        pll poomm=red.front();
        red.pop();
        ll tx=poomm.fi,ty=poomm.se;
        ll dx=1,dy=0;
        ll xx=tx+dx; ll yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist2[xx][yy]==0){
            dist2[xx][yy]=dist2[tx][ty]+1;
            red.push({xx,yy});
        }
        dx=-1,dy=0;
        xx=tx+dx; yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist2[xx][yy]==0){
            dist2[xx][yy]=dist2[tx][ty]+1;
            red.push({xx,yy});
        }
        dx=0,dy=1;
        xx=tx+dx; yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist2[xx][yy]==0){
            dist2[xx][yy]=dist2[tx][ty]+1;
            red.push({xx,yy});
        }
        dx=0,dy=-1;
        xx=tx+dx; yy=ty+dy;
        if(xx>=0 && xx<=n && yy>=0 && yy<=m && mat[xx][yy]==0 && dist2[xx][yy]==0){
            dist2[xx][yy]=dist2[tx][ty]+1;
            red.push({xx,yy});
        }
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>m;
    for(ll i=1;i<=n;i++){
        string aaa; cin>>aaa;
        for(ll j=1;j<=m;j++){
            if(aaa[j-1]=='.') mat[i][j]=0;
            else if(aaa[j-1]=='S'){
                st.fi=i; st.se=j; mat[i][j]=0;
            }else if(aaa[j-1]=='*') mat[i][j]=1;
            else{
                mat[i][j]=1;
                va.pb({i,j});
            }
        }
    }
    bfs1();
    bfs2();
    ll cnt=0;
    /*for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++) cerr<<dist1[i][j]<<" ";
        cerr<<"\n";
    }
    cerr<<"\n";
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++) cerr<<dist2[i][j]<<" ";
        cerr<<"\n";
    }*/
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=m;j++) if((dist1[i][j]<dist2[i][j] && dist1[i][j]!=0)||dist2[i][j]==0) cnt++;
    cout<<cnt;
    return 0;
}