#include <stdio.h>
#include <math.h>
#include <bits/stdc++.h>
#include <string>
#define ll long long
#include <iostream>

using namespace std;

void unos(ll N,ll M, vector<vector<char>> &lavirint)
{
	for(int i=0;i<N;i++)
	{
    	for(int j=0;j<M;j++)
		{
			cin >> lavirint[i][j];
		}  
	}
}

void pokreti(ll N,ll M, vector<vector<char>> &lavirint)
{
	vector<vector<char>> temp = lavirint;
	
	for(int i=0; i<N;i++)
	{
    	for(int j=0;j<M;j++)
		{
			if(lavirint[i][j] == 'S')
			{
				if(i+1 < N && lavirint[i+1][j] == '.')
					temp[i+1][j] = 'S';
				if(i-1 >= 0 && lavirint[i-1][j] == '.')
					temp[i-1][j] = 'S';
				if(j+1 < M && lavirint[i][j+1] == '.')
					temp[i][j+1] = 'S';
				if(j-1 >= 0 && lavirint[i][j-1] == '.')
					temp[i][j-1] = 'S';	
			}
			if(lavirint[i][j] == 'X')
			{
				if(i+1 < N && lavirint[i+1][j] == '.')
					temp[i+1][j] = 'X';
				if(i-1 >= 0 && lavirint[i-1][j] == '.')
					temp[i-1][j] = 'X';
				if(j+1 < M && lavirint[i][j+1] == '.')
					temp[i][j+1] = 'X';
				if(j-1 >= 0 && lavirint[i][j-1] == '.')
					temp[i][j-1] = 'X';	
			}
		}  
	}
	
	lavirint = temp;
}


void pokreti2(ll N, ll M, vector<vector<char>> &lavirint) {
    bool caosvima;
    do {
        vector<vector<char>> temp = lavirint; 
        pokreti(N, M, lavirint);              
        caosvima = (lavirint != temp);        
    } while (caosvima); 
	
	int x=0;
	
	for(int i=0;i<N;i++)
	{
    	for(int j=0;j<M;j++)
		{
			if(lavirint[i][j]=='S')
			x++;
		}  
	}
	
	cout << x;                      
}


/*void izlaz(ll N,ll M, vector<vector<char>> &lavirint)
{
	for(int i = 0; i < N; i++)
	{
    	for(int j = 0; j < M; j++)
		{
			cout << lavirint[i][j];
		}  
		cout << endl;
	}
}*/

main()
{
	ll N,M;
	
	cin >> N >> M;
	cin.ignore();
	
	vector<vector<char>> lavirint(N,vector<char>(M));
	
	unos(N,M,lavirint);
	pokreti2(N,M,lavirint);

//	izlaz(N,M,lavirint);
}