#include <iostream>
#include <string>
#include <vector>
using namespace std;
void SirenjeVatre(vector<vector<string>> l, int n, int m)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (l[i][j] == "X")
			{
				if (i > 0 and l[i - 1][j] != "*")
					l[i - 1][j] = "X";
				if (i < n - 1 and l[i + 1][j] != "*")
					l[i + 1][j] = "X";
				if (j > 0 and l[i][j - 1] != "*")
					l[i][j - 1] = "X";
				if (j < m - 1 and l[i][j + 1] != "*")
					l[i][j + 1] = "X";
			}
		}
	}
}
void StevinoKretanje(vector<vector<string>> l, int n, int m)
{
	int br = 1, pokusaji=0;
	bool promena = false;
	while (pokusaji < 7)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				promena = false;
				if (l[i][j] == "S")
				{
					if (i > 0 and l[i - 1][j] == ".")
					{
						l[i - 1][j] = "S";
						br++;
						promena = true;
					}
					if (i < n - 1 and l[i + 1][j] == ".")
					{
						l[i + 1][j] = "S";
						br++;
						promena = true;
					}
					if (j > 0 and l[i][j - 1] == ".")
					{
						l[i][j - 1] = "S";
						br++;
						promena = true;
					}
					if (j < m - 1 and l[i][j + 1] == ".")
					{
						l[i][j + 1] = "S";
						br++;
						promena = true;
					}
					if (promena == true)
						SirenjeVatre(l, n, m);
				}
			}
		}
		pokusaji++;
	}
	cout << br;
}
int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<string>> l(n, vector<string>(m));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> l[i][j];
		}
	}

	SirenjeVatre(l, n, m);
	StevinoKretanje(l, n, m);
	return 0;
}