#include <bits/stdc++.h>

using namespace std;

queue<pair<int, int>> fireQueue;
queue<pair<int, int>> steveQueue;
int total = 1;
int N, M;

void run(char *polje){
	queue<pair<int, int>> fireQueue0;
	queue<pair<int, int>> steveQueue0;
	
	while(!fireQueue.empty()){
	    auto t = fireQueue.front();
		int x = t.first;
		int y = t.second;
		
		if(x > 0) {
			if(*(polje + (x - 1) * M + y) == '.') {
				fireQueue0.push({x - 1, y});
				*(polje + (x - 1) * M + y) = 'X';
			}
		}
		
		if(x < N - 1){
			if(*(polje + (x + 1) * M + y) == '.') {
				fireQueue0.push({x + 1, y});
				*(polje + (x + 1) * M + y) = 'X';
			}
		}	
		
		if(y > 0) {
			if(*(polje + x * M + y - 1) == '.') {
				fireQueue0.push({x, y - 1});
				*(polje + x * M + y - 1) = 'X';
			}
		}
		
		if(y < M - 1){
			if(*(polje + x * M + y + 1) == '.') {
				fireQueue0.push({x, y + 1});
				*(polje + x * M + y + 1) = 'X';
			}
		}
				
		fireQueue.pop();
	}
	
	while(!steveQueue.empty()){
	    auto t = steveQueue.front();
		int x = t.first;
		int y = t.second;
		
		if(x > 0) {
			if(*(polje + (x - 1) * M + y) == '.') {
				steveQueue0.push({x - 1, y});
				total++;
				*(polje + (x - 1) * M + y) = 'X';
			}
		}
		
		if(x < N - 1){
			if(*(polje + (x + 1) * M + y) == '.') {
				steveQueue0.push({x + 1, y});
				total++;
				*(polje + (x + 1) * M + y) = 'X';
			}
		}	
		
		if(y > 0) {
			if(*(polje + x * M + y - 1) == '.') {
				steveQueue0.push({x, y - 1});
				total++;
				*(polje + x * M + y - 1) = 'X';
			}
		}
		
		if(y < M - 1){
			if(*(polje + x * M + y + 1) == '.'){
				steveQueue0.push({x, y + 1});
				total++;
				*(polje + x * M + y + 1) = 'X';
			} 
		}
				
		steveQueue.pop();
	}
	
	if(!steveQueue0.empty()){	
		fireQueue = fireQueue0;
		steveQueue = steveQueue0;
		run(polje);
	}
}

int main() {
	cin>>N>>M;
	
	char polje[N][M];
	
	for(int i = 0; i < N; i++){
		string s;
		cin>>s;
		int l = 0;
		for(char c : s){
			polje[i][l++] = c;
		
			switch(c){
				case 'S':
					steveQueue.push({i, l-1});
					break;
				case 'X':
					fireQueue.push({i, l-1});
					break;
			}
		}
	}
	
	run((char*) *polje);
	cout<<total;
	
	return 0;
}