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

int maze[1005][1005];
int arry[4] = {1,0,-1,0};
int arrx[4] = {0,1,0,-1};
int BFS(int starty,int startx)
{
	maze[starty][startx]=0;
	int rez=1;
	
	queue<int> y;
	queue<int> x;
	queue<int> val;
	y.push(starty);
	x.push(startx);
	val.push(0);
	//cout <<"asdf\n";
	while(!y.empty())
	{
		int pomy=y.front();
		int pomx=x.front();
		int pomval=val.front();
		y.pop();
		x.pop();
		val.pop();
		for(int i=0;i<4;i++)
		{
			if(pomval+1<maze[pomy+arry[i]][pomx+arrx[i]])
			{
				maze[pomy+arry[i]][pomx+arrx[i]]=pomval+1;
				rez++;
				y.push(pomy+arry[i]);
				x.push(pomx+arrx[i]);
				val.push(pomval+1);
			}
		}
		
	}
	return rez;
}

int main(int argc, char** argv) {
	ios_base::sync_with_stdio(false);
	
	int n,m;
	cin >>n>>m;
	for(int i=0;i<n+2;i++)
	{
		for(int j=0;j<m+2;j++)
		{
			maze[i][j]=-1;
		}
	}
	
	vector<pair<int,int> > v;
	pair<int,int> steva;
	//cout <<"sefefw\n";
	for(int i=1;i<n+1;i++)
	{
		string s;
		cin >>s;
		//cout <<i<<"fffff\n";
		for(int j=1;j<m+1;j++)
		{
			
			if(s[j-1]=='*')
			{
				maze[i][j]=-1;
			}
			else
			{
				maze[i][j]=INT_MAX;
			}
			if(s[j-1]=='X')
			{
				v.push_back(make_pair(i,j));
			}
			if(s[j-1]=='S')
			{
				steva=make_pair(i,j);
			}
		}
	}
	
	//cout <<"seg\n";
	for(int i=0;i<v.size();i++)
	{
		//cout <<v[i].first<<" "<<v[i].second<<"\n";
		BFS(v[i].first,v[i].second);
	}
	cout <<BFS(steva.first,steva.second)<<"\n";
	/*for(int i=0;i<n+2;i++)
	{
		for(int j=0;j<m+2;j++)
		{
			cout <<maze[i][j]<<" ";
		}
		cout <<"\n";
	}*/
	return 0;
}