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

int main()
{
	int n, m;
	int count = 0;
	char x;
	cin >> n >> m;
	vector<vector<char>> map(n, vector<char>(m));
	vector<vector<bool>> unchanged(n, vector<bool>(m, true));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> x;
			map[i][j] = x;
		}
	}
	bool spread = true;
	while (spread) {
		spread = false;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (map[i][j] == 'X') {
					if (i != 0 && map[i - 1][j] == '.' && unchanged[i][j]) {
						map[i - 1][j] = 'X';
						unchanged[i - 1][j] = false;
						spread = true;
					}
					if (i != n - 1 && map[i + 1][j] == '.' && unchanged[i][j]) {
						map[i + 1][j] = 'X';
						unchanged[i + 1][j] = false;
						spread = true;
					}
					if (j != 0 && map[i][j - 1] == '.' && unchanged[i][j]) {
						map[i][j - 1] = 'X';
						unchanged[i][j - 1] = false;
						spread = true;
					}
					if (j != m - 1 && map[i][j + 1] == '.' && unchanged[i][j]) {
						map[i][j + 1] = 'X';
						unchanged[i][j + 1] = false;
						spread = true;
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (map[i][j] == 'S') {
					if (i != 0 && map[i - 1][j] == '.' && unchanged[i][j]) {
						map[i - 1][j] = 'S';
						unchanged[i - 1][j] = false;
						spread = true;
					}
					if (i != n - 1 && map[i + 1][j] == '.' && unchanged[i][j]) {
						map[i + 1][j] = 'S';
						unchanged[i + 1][j] = false;
						spread = true;
					}
					if (j != 0 && map[i][j - 1] == '.' && unchanged[i][j]) {
						map[i][j - 1] = 'S';
						unchanged[i][j - 1] = false;
						spread = true;
					}
					if (j != m - 1 && map[i][j + 1] == '.' && unchanged[i][j]) {
						map[i][j + 1] = 'S';
						unchanged[i][j + 1] = false;
						spread = true;
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				unchanged[i][j] = true;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (map[i][j] == 'S') {
				count++;
			}
		}
	}
	cout << count;
}