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

int N; int M;

char lavvirint[10][10];  

int pozar[10][10]; 

int stevica[10][10]; 

int ggy[4] = {0, 0, -1, 1};  
int ggx[4] = {-1, 1, 0, 0};  
 

int main() {
   
    cin >> N;
    cin >> M;

    
    for (int l = 0; l < N; l++) {
        for (int j = 0; j < M; j++) {
            cin >> lavvirint[l][j];
            pozar[l][j] = -1;  
            stevica[l][j] = -1; 
        }
    }

    
    queue<pair<int, int>> pozarcic;
  
    for (int i = 0; i < N; i++) 
	{
       
	       for (int j = 0; j < M; j++)  
		   {
      if (lavvirint[i][j] == 'X') 
	  {
          pozarcic.push({i, j});
    pozar[i][j] = 0;
            }
        }
    }

         while (!pozarcic.empty())
		  {
     auto celija = pozarcic.front();
                    pozarcic.pop();
int x = celija.first, y = celija.second;
         for (int i = 0; i < 4; i++) 
		 {
         int nx = x + ggx[i], ny = y + ggy[i];
      if (nx >= 0 && nx < N && ny >= 0 && ny < M && lavvirint[nx][ny] == '.' && pozar[nx][ny] == -1) 
	  {
                pozar[nx][ny] = pozar[x][y] + 1;
                pozarcic.push({nx, ny});
             
		  }
       
	     }
     
	 }


       queue<pair<int, int>> stevicakretanje;
    
             for (int l = 0; l < N; l++)
	   {
       
	    for (int j = 0; j < M; j++)  
		{
           
		    if (lavvirint[l][j] == 'S') 
		{
                
			stevicakretanje.push({l, j});
                  stevica[l][j] = 0;
            }
        }
    }

      
	   
	    while (!stevicakretanje.empty()) 
		 
		 {
        
		auto celija = stevicakretanje.front();
       
	    stevicakretanje.pop();
        int x = celija.first, y = celija.second;
        for (int i= 0; i< 4;i++) 
		{
    int nx = x + ggx[i], ny = y + ggy[i];
             	 if (nx >= 0 && nx < N && ny >= 0 && ny < M && lavvirint[nx][ny] == '.' && stevica[nx][ny] == -1) {
                	 stevica[nx][ny] = stevica[x][y] + 1;
              	  stevicakretanje.push({nx, ny});
           }
       
	    }

}

   
    int brojacc = 0;
   
   
    for (int i = 0; i < N; i++)
	 {
       
	    for (int ll = 0; ll < M; ll++) {
          
		    if (stevica[i][ll] != -1 && (pozar[i][ll] == -1 || stevica[i][ll] < pozar[i][ll])) {
                brojacc++;
           
		    }
       
	    } }

   
   
  	  cout << brojacc << endl;
   
   
   
   
  
    return 0;
}