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

char matricaaa[14][14];
int vreme__vatre[14][14];
int N, M;
int pera_vreme[14][14];

int AA[] = {-1, 1, 0, 0};
int BB[] = {0, 0, -1, 1};

void Sir_vatra() {
    for (int i = 0; i < N; i++)  { for (int j = 0; j < M; j++)  {  vreme__vatre[i][j] = -1;
        } }
    for (int qq = 0; qq < N; qq++) 
	{ for (int ww = 0; ww < M; ww++) 
		{ if (matricaaa[qq][ww] == 'X') 
			{ vreme__vatre[qq][ww] = 0;
            } } }
 bool promenicabobica = true;
while (promenicabobica) {  promenicabobica = false;
for (int kk = 0; kk < N; kk++)  						{
for (int vv = 0; vv < M; vv++)
 		{
if (vreme__vatre[kk][vv] != -1) 		
	{ 
  for (int d = 0; d < 4; d++) 
  		{
    int ni = kk + AA[d], nj = vv + BB[d];
                       						 if (ni >= 0 && ni < N && nj >= 0 && nj < M && matricaaa[ni][nj] == '.' && vreme__vatre[ni][nj] == -1) 
												{
                           					 vreme__vatre[ni][nj] = vreme__vatre[kk][vv] + 1;
                           						 promenicabobica = true;
                        }} }} }}}

						void pericalaganica(int si, int sj) {
    							for (int i = 0; i < N; i++)   {
       									 for (int j = 0; j < M; j++)    {
           										 pera_vreme[i][j] = -1;
        }}
    pera_vreme[si][sj] = 0;

    bool promenicabobica = true;
    while (promenicabobica) {
        promenicabobica = false;
          for (int qq = 0; qq < N; qq++) 
	{ for (int ww = 0; ww < M; ww++)  {
                if (pera_vreme[qq][ww] != -1) {
                    for (int d = 0; d < 4; d++) {
                        int ni = qq + AA[d], nj = ww + BB[d];
                        if (ni >= 0 && ni < N && nj >= 0 && nj < M && matricaaa[ni][nj] == '.' && pera_vreme[ni][nj] == -1) {
                            pera_vreme[ni][nj] = pera_vreme[qq][ww] + 1;
                            promenicabobica = true; }}}} }}}
	int brojidostupne() {
    int dostupnicapica = 0;
    for (int i = 0; i < N; i++) 					{
        for (int j = 0; j < M; j++) 				{ if (pera_vreme[i][j] != -1 && (vreme__vatre[i][j] == -1 || pera_vreme[i][j] < vreme__vatre[i][j])) {
                dostupnicapica++; } }}
    return dostupnicapica;
}

int main() {
    cin >> N >> M;
    
    pair<int, int> peralegendica;
    for (int qq = 0; qq < N; qq++) {
        for (int z = 0; z < M; z++) {
            cin >> matricaaa[qq][z];
            if (matricaaa[qq][z] == 'S') {
                peralegendica = {qq, z};
            }
        }
    }

    Sir_vatra(); 
    pericalaganica(peralegendica.first, peralegendica.second); 
			  cout << brojidostupne() << endl; 
    return 0;
}