#include <iostream>
#include <map>
#include <vector>

using namespace std;

void sirenjeVatre(vector<string> mapa) {
    int time = 0;
}

int main() {
    int n, m, r = 1;
    cin >> n >> m;
    vector<string> mapa;
    string temp;
    for(int i = 0; i < n; i++) { cin >> temp; mapa.push_back(temp); }
    if(n == 1) {
        int l = -1, d = -1, lz = -1, dz = -1, s = -1;
        bool left = false, right = false;
        for(int i = 0; i < m; i++) {
            if(left == false && mapa[0][i]=='X') l = i+1; 
            else if(mapa[0][i] == 'S') {
                left = true;
                s = i+1;
            }
            else if(right == false && mapa[0][i]=='X') {
                d = i+1;
                right = true;
            }
            else if(mapa[0][i] == '*' && left == false) lz = i+1;
            else if(mapa[0][i] == '*' && right == false) {
                dz = i+1;
                right = true;
            }
            if(right) break;
        }
        if(lz != -1 && l != -1) (lz > l) ? r+= s-lz-1 : r+=(s-l-1)/2;
        else if(lz != -1) r+= s-lz-1;
        else if(l!=-1) r+=(s-l-1)/2;
        else r+= s-1;
        if(dz != -1 && d != -1) (dz < d) ? r+= dz-s-1 : r+=(d-s-1)/2;
        else if(dz != -1) r+= dz-s-1;
        else if(d!=-1) r+=(d-s-1)/2;
        else r+= m-s;
    }

    cout << r;

    return 0;
}