#include <iostream> 
#include <list>
using namespace std;
char a[10][10];
unsigned redovi, kolone, br = 0;
list <pair<int, int> > Zapaliti, Steva;
list <pair<int, int> >::iterator kraj, tr, pom;
bool iskoriscen[10][10];

void ProsiriPolje(int i, int j, char c) {
	
	if (i - 1 >= 0 && !iskoriscen[i - 1][j] && a[i - 1][j] == '.') {
		a[i - 1][j] = c;
		iskoriscen[i - 1][j] = true;
		if (c == 'X') Zapaliti.push_back(make_pair(i - 1, j));
		if (c == 'S') {
			br++;
			Steva.push_back(make_pair(i - 1, j));
		}
	}
	
	if (j - 1 >= 0 && !iskoriscen[i][j - 1] && a[i][j - 1] == '.') {
		a[i][j - 1] = c;
		iskoriscen[i][j - 1] = true;
		if (c == 'X') Zapaliti.push_back(make_pair(i, j - 1));
		if (c == 'S') {
			br++;
			Steva.push_back(make_pair(i, j - 1));
		}
	}
	
	if (i + 1 < redovi && !iskoriscen[i + 1][j] && a[i + 1][j] == '.') {
		a[i + 1][j] = c;
		iskoriscen[i + 1][j] = true;
		if (c == 'X') Zapaliti.push_back(make_pair(i + 1, j));
		if (c == 'S') {
			br++;
			Steva.push_back(make_pair(i + 1, j));
		}
	}
	
	if (j + 1 < kolone && !iskoriscen[i][j + 1] && a[i][j + 1] == '.') {
		a[i][j + 1] = c;
		iskoriscen[i][j + 1] = true;
		if (c == 'X') Zapaliti.push_back(make_pair(i, j + 1));
		if (c == 'S') {
			br++;
			Steva.push_back(make_pair(i, j + 1));
		}
	}

}

void ProsiriVatru() {
	int pon = Zapaliti.size();
	tr = Zapaliti.begin();
	while (pon > 0) {
		ProsiriPolje(tr->first, tr->second, 'X');
		tr = Zapaliti.erase(tr);
		pon--;
	}
}

void ProsiriStevu() {
	int pon = Steva.size();
	tr = Steva.begin();
	while (pon > 0) {
		ProsiriPolje(tr->first, tr->second, 'S');
		tr = Steva.erase(tr);
		pon--;
	}
}

void Korak() {
	while (true) {
		int n = br;
		ProsiriVatru();
		ProsiriStevu();
		//Ispis();
		if (br == n) break;
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m, x = 0, y = 0;
	cin >> n >> m;
	redovi = n;
	kolone = m;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] == 'X') {
				Zapaliti.push_back(make_pair(i, j));
				iskoriscen[i][j] = true;
			}

			if (a[i][j] == 'S') {
				Steva.push_back(make_pair(i, j));
				br++;
				iskoriscen[i][j] = true;
			}
		}
	}
	
	Korak();
	cout << br;
}