#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

int main()
{
    vector<vector<int> > lav(1002,vector<int>(1002,-1));
    vector<int> x(1000006,0);
    vector<int> y(1000006,0);
    int n,m,xs,ys,brv=0;
    string red;
    cin >> n >> m;//n za broj redova a m za duzinu
    for(int i=0;i<n;i++){
        cin >> red;
        for(int j=0;j<m;j++){
            if(red[j]=='.'){
                lav[i+1][j+1]=0;
            }
            else if(red[j]=='*'){
                lav[i+1][j+1]=-1;
            }
            else if(red[j]=='S'){
                lav[i+1][j+1]=1;
                xs=i+1; ys=j+1;
            }
            else{
                lav[i+1][j+1]=2;
                x[brv]=i+1; y[brv]=j+1;
                brv++;
            }
        }
    }
    queue<int> qsx,qsy,qvx,qvy;
    int brojac=1;
    for(int i=0;i<brv;i++){
        qvx.push(x[i]);
        qvy.push(y[i]);
    }
    qsx.push(xs);
    qsy.push(ys);
    while(!qsx.empty()){
        int sz=qvx.size();
        while(sz--){
            int tx=qvx.front(),ty=qvy.front();
            qvx.pop(); qvy.pop();
            if(lav[tx+1][ty]==0){
                lav[tx+1][ty]=2;
                qvx.push(tx+1); qvy.push(ty);
            }
            if(lav[tx-1][ty]==0){
                lav[tx-1][ty]=2;
                qvx.push(tx-1); qvy.push(ty);
            }
            if(lav[tx][ty+1]==0){
                lav[tx][ty+1]=2;
                qvx.push(tx); qvy.push(ty+1);
            }
            if(lav[tx][ty-1]==0){
                lav[tx][ty-1]=2;
                qvx.push(tx); qvy.push(ty-1);
            }
        }
        sz=qsx.size();
        while(sz--){
            int tx=qsx.front(),ty=qsy.front();
            qsx.pop(); qsy.pop();
            if(lav[tx+1][ty]==0){
                lav[tx+1][ty]=2;
                qsx.push(tx+1); qsy.push(ty);
            }
            if(lav[tx-1][ty]==0){
                lav[tx-1][ty]=2;
                qsx.push(tx-1); qsy.push(ty);
            }
            if(lav[tx][ty+1]==0){
                lav[tx][ty+1]=2;
                qsx.push(tx); qsy.push(ty+1);
            }
            if(lav[tx][ty-1]==0){
                lav[tx][ty-1]=2;
                qsx.push(tx); qsy.push(ty-1);
            }
        }
        brojac+=qsx.size();
    }
    cout << brojac << endl;
    return 0;
}