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

using namespace std;


void BFS(int i, int j, int n, int m, vector<vector<char>>& matrica, vector<vector<int>>& rastojanje, queue<pair<int, int>> red){
//	queue<pair<int, int> > red;
//	red.emplace(i, j);
	rastojanje[i][j] = 0;
	
	while(!red.empty()){
		int x = red.front().first;
		int y = red.front().second;
		
//		matrica[x][y] = 'X';
		red.pop();
		
		int delta_x[] = {-1, 0, 1, 0};
		int delta_y[] = {0, 1, 0, -1};
	
		for(int smer = 0; smer < 4; smer++){
			int novo_x = x + delta_x[smer];
			int novo_y = y + delta_y[smer];
			if(novo_x >= 0 and novo_x < n and novo_y >= 0 and novo_y < m and matrica[novo_x][novo_y] != '*' and rastojanje[novo_x][novo_y] == -1){
				red.emplace(novo_x, novo_y);
				rastojanje[novo_x][novo_y] = rastojanje[x][y] + 1;
			}
		} 
	}
}

int main(){
	int n, m;
	cin >> n >> m;
	
	vector<vector<char>> matrica(n, vector<char>(m));
    vector<vector<int>> rastojanja_vatra(n, vector<int>(m, -1));
    vector<vector<int>> rastojanja_steva(n, vector<int>(m, -1));
	int steva_x, steva_y;
	queue<pair<int, int>> red_vatra;
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			cin >> matrica[i][j];
			if(matrica[i][j] == 'S'){
				steva_x = i;
				steva_y = j;
			}
			else if(matrica[i][j] == 'X'){
				red_vatra.emplace(i, j);
				rastojanja_vatra[i][j] = 0;
			}
		}
	}
	
//	while(!red_vatra.empty()){
//		int i = red_vatra.front().first;
//		int j = red_vatra.front().first;
//		red_vatra.pop();
//		BFS(i, j, n, m, matrica, rastojanja_vatra);
//	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(matrica[i][j] == 'X'){
				BFS(i, j, n, m, matrica, rastojanja_vatra, red_vatra);
			}	
		}
	}
	queue<pair<int, int>> red_steva;
	red_steva.emplace(steva_x, steva_y);
	BFS(steva_x, steva_y, n, m, matrica, rastojanja_steva, red_steva);
	int brojac = 0;
//	for(int i = 0; i < n; i++){
//		for(int j = 0; j < m; j++){
//			cout << rastojanja_steva[i][j] << " ";
//			}
//			cout << endl;
//		}
//		cout << endl;
//		for(int i = 0; i < n; i++){
//		for(int j = 0; j < m; j++){
//			cout << rastojanja_vatra[i][j] << " ";
//			}
//			cout << endl;
//		}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(matrica[i][j] != '*' && (rastojanja_vatra[i][j] > rastojanja_steva[i][j] and rastojanja_steva[i][j] != -1) || (rastojanja_vatra[i][j] == -1 && rastojanja_steva[i][j] > -1)){
				brojac++;
			}
		}
	}
	cout << brojac;

	return 0;
}