#include <bits/stdc++.h>

using namespace std;

char lavirint[1005][1005];
char niz[1005];
int s[1005];
int v[1005];
int main(){
    int n, m, indexs, indexv1, indexv2;
    int mini1 = 1005;
    int mini2 = 1005;

    cin >> n >> m;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> lavirint[i][j];
        }
    }

    niz[0] = '*';
    niz[n+1] = '*';

    for(int i = 1; i <= m; i++){
        if(niz[i] == 'S')
            indexs = i;
    }

    for(int i = 1; i <= m; i++){
        s[i] = abs(i - indexs);
    }

    for(int i = 1; i <= m; i++){
        if(niz[i] == 'X' && i < indexs)
            if(abs(i - indexs) < mini1){
            mini1 = i;
            indexv1 = i;
            }

        if(niz[i] == 'X' && i > indexs)
            if(abs(i - indexs) < mini2){
            mini2 = i;
            indexv2 = i;
            }
    }

    cout << 0 << endl;

    return 0;
}