#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> os;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1005;
const int maxM = 1005;
const int inf = INT_MIN;
int a[maxN][maxM], dist[maxN][maxM], dist1[maxN][maxM];
bool visited[maxN][maxM];
void dfs(int i, int j, int n, int m, int tr){
    
    dist[i][j]=tr;
    tr++;
    if(i+1<n&&dist[i+1][j]>tr)dfs(i+1, j, n, m, tr);
    if(i-1>=0&&dist[i-1][j]>tr)dfs(i-1, j, n, m, tr);
    if(j-1>=0&&dist[i][j-1]>tr)dfs(i, j-1, n, m, tr);
    if(j+1<m&&dist[i][j+1]>tr)dfs(i, j+1, n, m, tr);
        
    
}
void dfs1(int i, int j, int n, int m, int tr){
        
    visited[i][j]=true;
    tr++;
    if(i+1<n&&dist[i+1][j]>tr&&!visited[i+1][j])dfs1(i+1, j, n, m, tr);
    if(i-1>=0&&dist[i-1][j]>tr&&!visited[i-1][j])dfs1(i-1, j, n, m, tr);
    if(j-1>=0&&dist[i][j-1]>tr&&!visited[i][j-1])dfs1(i, j-1, n, m, tr);
    if(j+1<m&&dist[i][j+1]>tr&&!visited[i][j+1])dfs1(i, j+1, n, m, tr);
}
int main(){
    ios;
    int n, m;
    cin>>n>>m;
    int is = 0;
    int js = 0;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        for(int j = 0;j<m;j++){
            visited[i][j]=false;
            if(s[j]=='.')a[i][j]=0;
            if(s[j]=='S'){
                a[i][j]=0;
                dist[i][j]=INT_MAX;
                is=i;
                js=j;
            }
            if(s[j]=='*')a[i][j]=1; //zid
            if(s[j]=='X')a[i][j]=2; //vatra
            if(s[j]=='*'){
                dist[i][j]=inf;
                visited[i][j]=true;
            }
            if(s[j]=='.')dist[i][j]=INT_MAX;
        }
    }
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            if(a[i][j]==2){
                dfs(i, j, n, m, 0);
            }
        }
    }
    dfs1(is, js, n, m, 0);
    int res = 0;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            //cout<<dist[i][j]<<" ";
            if(visited[i][j]&&a[i][j]!=1)res++;
        }
        //cout<<endl;
    }
    cout<<res<<endl;

}