#include <bits/stdc++.h>

#define int long long 

#define pb push_back
 
#define F first
 
#define S second
 
using namespace std;
 
const int N = 1000 + 10;

const int mod = 1e9 + 7;

const int inf = 1e9;

int dp[N][N][4],dist[N][N],dist2[N][N];
char a[N][N];
int xx[4] = {1,0,-1,0};
int yy[4] = {0,1,0,-1};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

 	int n,m,x,y;
 	cin >> n >> m;
 	for(int i = 1; i <= n; i++) {
 		for(int j = 1; j <= m; j++) {
 			cin >> a[i][j];
 			dist[i][j] = inf;
 			dist2[i][j] = inf;
 			if(a[i][j] == 'X') dist2[i][j] = 0;
 			if(a[i][j] == 'S') {
 				x = i;
 				y = j;
 				dist[i][j] = 0;
 			}
 		}
 	}
 	queue<pair<int,int>>q;
 	q.push({x,y});
 	while(!q.empty()) {
 		int X = q.front().F,Y = q.front().S;
 		q.pop();
 		for(int i = 0; i < 4; i++) {
 			int nx = X + xx[i],ny = Y + yy[i];
 			if(nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
 				if(a[nx][ny] == '.' && dist[nx][ny] == inf) {
 					dist[nx][ny] = dist[X][Y] + 1;
 					q.push({nx,ny});
 				}
 			}
 		}
 	}
 	for(int i = 1; i <= n; i++) {
 		for(int j = 1; j <= m; j++) {
 			if(a[i][j] == 'X') {
 				q.push({i,j});
 			}
 		}
 	}
 	while(!q.empty()) {
 		int X = q.front().F,Y = q.front().S;
 		q.pop();
 		for(int i = 0; i < 4; i++) {
 			int nx = X + xx[i],ny = Y + yy[i];
 			if(nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
 				if(a[nx][ny] == '.' && dist2[nx][ny] == inf) {
 					dist2[nx][ny] = dist2[X][Y] + 1;
 					q.push({nx,ny});
 				}
 			}
 		}
 	}
 	int res = 1;
 	for(int i = 1; i <= n; i++) {
 		for(int j = 1; j <= m; j++) {
 			if(a[i][j] == '.' && dist[i][j] < dist2[i][j]) {
 				res += 1;
 			}
 		}
 	}
 	cout << res;
    return 0;
}