#include <iostream>
#include <queue>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, m;
	cin >> n >> m;
	
	char lavirint[n][m];
	queue<pair<int, int> > red;
	pair<int, int> steve;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			cin >> lavirint[i][j];
			if(lavirint[i][j] == 'X')
				red.emplace(i, j);
			if(lavirint[i][j] == 'S')
				steve = make_pair(i, j);
		}
	}
	red.push(steve);
	
	int polja = 1;
	while(!red.empty()) {
		int x = red.front().first,
		    y = red.front().second;
		
		int dx[] = {-1, 1, 0, 0},
		    dy[] = { 0, 0,-1, 1};
		
		for(int i = 0; i < 4; i++) {
			if(x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m) {
				if(lavirint[x+dx[i]][y+dy[i]] == '.') {
					lavirint[x+dx[i]][y+dy[i]] = lavirint[x][y];
					red.emplace(x+dx[i], y+dy[i]);
					if(lavirint[x][y] == 'S')
						polja++;
				}
			}
		}
		
		red.pop();
	}
	
	cout << polja << endl;
	
	return 0;
}