#include<bits/stdc++.h>

using namespace std;

const int MAX = 1000;
int m, n;
char polje[MAX][MAX]; bool obelezeno[MAX][MAX] = {0};

int PotezS(int Stefa_x, int Stefa_y) {
	queue<pair<int, int>> Stefa;
    Stefa.emplace(Stefa_x, Stefa_y);
    obelezeno[Stefa_x][Stefa_y] = 1;
    
    int brojac = 1;
  
	while(!Stefa.empty()) {

        int smer_x[] = {-1, 0, 1, 0};
        int smer_y[] = {0, -1, 0, 1};
        
        auto s = Stefa.front(); Stefa.pop();
        int x = s.first;
        int y = s.second;

        for(int j = 0; j < 4; j++) {
            int nx = x + smer_x[j];
            int ny = y + smer_y[j];
                

            if(nx > -1 and nx < n and ny > -1 and ny < m and obelezeno[nx][ny] == 0) {
                Stefa.emplace(nx, ny);
                obelezeno[nx][ny] = 1;
                brojac++;
            }
            }
    
	}
	return brojac;
	
}

int Potez(int Stefa_x, int Stefa_y, int Vatra_x, int Vatra_y) {
    
    queue<pair<int, int>> Stefa;
    Stefa.emplace(Stefa_x, Stefa_y);
    obelezeno[Stefa_x][Stefa_y] = 1;
    
    queue<pair<int,int>> Vatra;
    Vatra.emplace(Vatra_x, Vatra_y);
    obelezeno[Vatra_x][Vatra_y] = 1;

    int brojac = 1;
    int pokusaji;

    while(!Stefa.empty()) {

        int smer_x[] = {-1, 0, 1, 0};
        int smer_y[] = {0, -1, 0, 1};

        pokusaji = Vatra.size();

        for(int i = 0; i < pokusaji; i++) {

            auto v = Vatra.front(); Vatra.pop();
            int x = v.first;
            int y = v.second;

            for(int j = 0; j < 4; j++) {
                int nx = x + smer_x[j];
                int ny = y + smer_y[j];

                if(nx > -1 and nx < n and ny > -1 and ny < m and obelezeno[nx][ny] == 0) {
                    Vatra.emplace(nx, ny);
                    obelezeno[nx][ny] = 1;
                }
            }
        } 

        pokusaji = Stefa.size();

        for(int i = 0; i < pokusaji; i++) {

            auto s = Stefa.front(); Stefa.pop();
            int x = s.first;
            int y = s.second;

            for(int j = 0; j < 4; j++) {
                int nx = x + smer_x[j];
                int ny = y + smer_y[j];

                if(nx > -1 and nx < n and ny > -1 and ny < m and obelezeno[nx][ny] == 0) {
                    Stefa.emplace(nx, ny);
                    obelezeno[nx][ny] = 1;
                    brojac++;
                }
            }
        }
    }

    return brojac;
}

int main(){

    cin >> n >> m;
    int Stefa_x = -1, Stefa_y = -1;
    int Vatra_x = -1, Vatra_y = -1;
 
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> polje[i][j];
            if (polje[i][j] == '*')
                obelezeno[i][j] = 1;
            else if (polje[i][j] == 'S') {
                Stefa_x = i;
                Stefa_y = j;
            }
            else if (polje[i][j] == 'X') {
                Vatra_x = i;
                Vatra_y = j;
            }
        }
    }
    
    int brojac;

	if (Vatra_x != -1 and Vatra_y != -1)
    	brojac = Potez(Stefa_x, Stefa_y, Vatra_x, Vatra_y);
    else
    	brojac = PotezS(Stefa_x, Stefa_y);

    cout << brojac; 

}