#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forn1(i, n) for (int i = 1; i < int(n); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define MOD 1000000007 // 998244353
#define FIO                \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

bool isValid(int x, int y, int n, int m)
{
	return x >= 0 && y >= 0 && x < n && y < m;
}

int main()
{
	FIO
	int n, m; cin >> n >> m;
	vector<vector<char>> a(n, vector<char>(m));	
	queue<pair<int, int>> s, v;
	vector<vector<int>> visited(n, vector<int>(m, 0));
	forn(i, n)
		forn(j, m)
		{
			cin >> a[i][j];
			if (a[i][j] == 'S')
				s.push({i, j});
			else if (a[i][j] == 'X')
				v.push({i, j});
			else if (a[i][j] == '*')
				visited[i][j] = 1;
		}
	int dx[] = {-1, 0, 0, 1};
	int dy[] = {0, -1, 1, 0};
	ll score = 1;
	while(!s.empty())
	{
		queue<pair<int, int>> v1;
		while(!v.empty())
		{
			int x, y;
			tie(x, y) = v.front();
			visited[x][y] = 1;
			v.pop();
			forn(i, 4)
			{
				int xx = x + dx[i];
				int yy = y + dy[i];
				if (isValid(xx, yy, n, m) && visited[xx][yy] != 1)
				{
					v1.push({xx, yy});
					visited[xx][yy] = 1;
				}		
			}
		}
		queue<pair<int, int>> s1;
		while(!s.empty())
		{
			int x, y;
			tie(x, y) = s.front();
			s.pop();
			if (visited[x][y] == 1)
				continue;
			visited[x][y] = 1;
			forn(i, 4)
			{
				int xx = x + dx[i];
				int yy = y + dy[i];
				if (isValid(xx, yy, n, m) && visited[xx][yy] == 0)
				{
					s1.push({xx, yy});
					visited[xx][yy] = -1;
					score++;
				}
			}
		}
		s = s1;
		v = v1;
	}
	cout << score << endl;
}