#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main(){
	int n, m;
	cin >> n >> m;
	char lavirint[ n ][ m ];
	
	int brojacV = 0;
	int brojacS = 1;
	int Steva = 1;
	
	queue < pair<int, int> > poljaV;
	queue < pair<int, int> > poljaS;
	
	for ( int i = 0; i < m; i++ ){
		for ( int j = 0; j < n; j++ ){
			
			cin >> lavirint[ i ][ j ];
			
			if ( lavirint[ i ][ j ] == 'X' ){
				poljaV.push( make_pair ( i, j ) );
				brojacV++;
			}
			
			if ( lavirint[ i ][ j ] == 'S' ){
				poljaS.push( make_pair ( i, j ) );
			}
		}
	}
	
	int Px[] = { 1, -1, 0, 0 };
	int Py[] = { 0, 0, 1, -1 };
	
	while ( !poljaV.empty() and !poljaS.empty() ){
		
		int maxV = brojacV;
		brojacV = 0;
		
		for ( int i = 0; i < maxV; i++ ){
			
			if ( maxV == 0 ){
				break;
			}
			
			int X = poljaV.front().first;
			int Y = poljaV.front().second;
			
			for ( int j = 0; j < 4; j++ ){
				if ( lavirint[ X + Px[j] ][ Y + Py[j] ] == '.' ){
					lavirint[ X + Px[j] ][ Y + Py[j] ] = 'X';
					brojacV++;
				}
			}
			
			poljaV.pop();
		}
		
		int maxS = brojacS;
		brojacS = 0;
		
		for ( int i = 0; i < maxS; i++ ){
			
			if ( maxS == 0 ){
				break;
			}
			
			int X = poljaS.front().first;
			int Y = poljaS.front().first;
			
			for ( int j = 0; j < 4; j++ ){
				
				if ( lavirint[ X + Px[j] ][ Y + Py[j] ] == '.' ){
					
					lavirint[ X + Px[j] ][ Y + Py[j] ] = 'S';
					brojacS++;
					Steva++;
				}
			}
			
			poljaS.pop();
		}
	}
	
	cout << Steva;
	
	return 0;
}