#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
template <class T> using indexed_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif



#define ar array
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define sort(a) sort(all(a))
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
const char nl = '\n';
const long long int ll_MAX=9223372036854775807;
const string deb = "------------";
//[x,y],[a,b]
int stepenDvojke(int n) {
    int s = 1;
    while (s < n)
        s <<= 1;
    return s;
}
int n;
int m;
bool check(vector<int> fr,vector<int> a[],vector<vector<bool> > visited,bool tk){
	if (fr[0] < 0 || fr[0] >= n || fr[1] < 0 || fr[1] >= m) return false; 
    if (!visited[fr[0]][fr[1]] && a[fr[0]][fr[1]] != -1 && (a[fr[0]][fr[1]] != 1 || tk)) 
        return true;
    return false;
	
	
	
}
void traversal(vector<int> a[],pair<int,int> start,int n,int m,vector<vector<pair<int,int> > > &s){
	vector<vector<bool>> visited(n, vector<bool>(m, false));
	vector<int> t = {start.first,start.second,0};
   visited[t[0]][t[1]]=true;
   s[t[0]][t[1]].first=0;
   s[t[0]][t[1]].second=1;
   queue<vector<int> >q;
   q.push(t);
   while(!q.empty()){
   		
   		auto fr = q.front();
   		q.pop();
   		if(s[fr[0]][fr[1]].second==5){
   		if(fr[2]<=s[fr[0]][fr[1]].first)s[fr[0]][fr[1]] = {fr[2],1};
   		}
   		if(fr[0]-1>=0){
   		if(check({fr[0]-1,fr[1]},a,visited,true)){
   			q.push({fr[0]-1,fr[1],fr[2]+1});
   			visited[fr[0]-1][fr[1]]=true;
   		}}
   		if(fr[0]+1<n ){
   		if(check({fr[0]+1,fr[1]},a,visited,true)){
   			q.push({fr[0]+1,fr[1],fr[2]+1});
   			visited[fr[0]+1][fr[1]]=true;
   		}}
   		if(fr[1]-1>=0 ){
   		if(check({fr[0],fr[1]-1},a,visited,true)){
   			q.push({fr[0],fr[1]-1,fr[2]+1});
   			visited[fr[0]][fr[1]-1]=true;
   		}}
   		if(fr[1]+1<m ){
   		if(check({fr[0],fr[1]+1},a,visited,true)){
   			q.push({fr[0],fr[1]+1,fr[2]+1});
   			visited[fr[0]][fr[1]+1]=true;
   		}}
   }
	
	
	
	
	
}





void solve() {
cin>>n>>m;
int cnt =0;
   vector<int> a[n];
   vector<pair<int,int> > v;
   pair<int,int> start;
	vector<vector<pi>> s(n, vector<pi>(m, {100000000, 5}));
   
   for(int i =0;i<n;i++){
   		for(int j = 0;j<m;j++){
   			char c;cin>>c;
   			if(c=='.')a[i].pb(0);
   			if(c=='S'){a[i].pb(5);start.first=i;start.second = j;}
   			if(c=='*')a[i].pb(-1);
   			if(c=='X'){a[i].pb(1);v.pb({i,j});}
   			
   		}
   	
   }
vector<vector<bool>> visited(n, vector<bool>(m, false));
  
   vector<int> t = {start.first,start.second,0};
   visited[t[0]][t[1]]=true;
   s[t[0]][t[1]].first=0;
   s[t[0]][t[1]].second=5;
   queue<vector<int> >q;
   q.push(t);
   while(!q.empty()){
   	
   		auto fr = q.front();
   		s[fr[0]][fr[1]] = {fr[2],5};
   		q.pop();
   		if(fr[0]-1>=0){
   		if(check({fr[0]-1,fr[1]},a,visited,false)){
   			q.push({fr[0]-1,fr[1],fr[2]+1});
   			visited[fr[0]-1][fr[1]]=true;
   		}}
   		if(fr[0]+1<n ){
   		if(check({fr[0]+1,fr[1]},a,visited,false)){
   			q.push({fr[0]+1,fr[1],fr[2]+1});
   			visited[fr[0]+1][fr[1]]=true;
   		}}
   		if(fr[1]-1>=0 ){
   		if(check({fr[0],fr[1]-1},a,visited,false)){
   			q.push({fr[0],fr[1]-1,fr[2]+1});
   			visited[fr[0]][fr[1]-1]=true;
   		}}
   		if(fr[1]+1<m ){
   		if(check({fr[0],fr[1]+1},a,visited,false)){
   			q.push({fr[0],fr[1]+1,fr[2]+1});
   			visited[fr[0]][fr[1]+1]=true;
   		}}
   }
   for(auto i : v){
   	//vector<int> a[],pair<int,int> start,int n,int m,vector<vector<int> > &s
   	traversal(a,i,n,m,s);
   }
   for(int i =0;i<n;i++){
   	for(int j=0;j<m;j++){
   		if(s[i][j].second==5 && a[i][j]!=-1)cnt++;
   	}
   }
   cout<<cnt;
   

   
   
   



}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t << ": ";
        solve();
    }
}