#include <iostream>
#include <vector>
#include <string>
using namespace std;

#define mnmax 1000
char mapa[mnmax][mnmax];
int N, M;
int vatre[mnmax][3];
int br_v = 0;

int steve[mnmax][3];
int br_s = 0;


void pokret(int* x, int& br, int (*y)[3], char c)
{
	int temp = br;
	if (mapa[x[0]][x[1] + 1] == '.' && x[1] != N - 1) // desno
	{
		mapa[x[0]][x[1] + 1] = c;
		y[br][0] = x[0];
		y[br][1] = x[1] + 1;
		y[br][2] = 0;
		br++;
	}
	if (mapa[x[0]][x[1] - 1] == '.' && x[1] != 0) // levo
	{
		mapa[x[0]][x[1] - 1] = c;
		y[br][0] = x[0];
		y[br][1] = x[1] - 1;
		y[br][2] = 0;
		br++;
	}
	if(mapa[x[0] - 1][x[1]] == '.' && x[0] != 0) // gore
	{
		mapa[x[0] - 1][x[1]] = c;
		y[br][0] = x[0] - 1;
		y[br][1] = x[1];
		y[br][2] = 0;
		br++;
	}
	if (mapa[x[0] + 1][x[1]] == '.' && x[0] != M- 1) // dole
	{
		mapa[x[0] + 1][x[1]] = c;
		y[br][0] = x[0] + 1;
		y[br][1] = x[1];
		y[br][2] = 0;
		br++;
	}
	if(temp == br)
	{
		x[2] = 1;
	}
}

int main() 
{
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin >> N >> M;

	int br_z = 0;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			cin >> mapa[i][j];
			if (mapa[i][j] == 'S')
			{
				steve[br_s][0] = i;
				steve[br_s][1] = j;
				steve[br_s][2] = 0;
				br_s++;
			}
			else if (mapa[i][j] == 'X')
			{
				vatre[br_v][0] = i;
				vatre[br_v][1] = j;
				vatre[br_v][2] = 0;
				br_v++;
			}
			else if (mapa[i][j] == '*')
			{
				br_z++;
			}
		}
	}
	
	if (br_v == 0)
	{
		cout << M * N - br_z;
	}
	else
	{
		while (true)
		{
			int temp = br_v;
			for (int i = 0; i < temp; i++)
			{
				if (vatre[i][2] == 0)
					pokret(vatre[i], br_v, vatre, 'X');
			}
			temp = br_s;
			bool tempb = true;
			for (int i = 0; i < temp; i++)
			{
				if (steve[i][2] == 0)
				{
					pokret(steve[i], br_s, steve, 'S');
					tempb = false;
				}

			}
			if (tempb)
			{
				break;
			}
		}
		cout << br_s;
	}
}