#include<iostream>
#include<string>
using namespace std;
void SirenjeVatre(string l[][1000], 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";
			}
		}
	}
}
int StevinoKretanje(string l[][1000], int n, int m)
{
	int br = 0;
	bool promena = false;
	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, m, n);
			}
		}
	}
	return br;
}
int main()
{
	int i, j, n, m;
	cin >> n >> m;
	string l[1000][1000];
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			cin >> l[i][j];
		}
	}
	int br = 0;
	SirenjeVatre(l, n, m);
	cout << StevinoKretanje(l, m, n);
	return 0;
}