#include <bits/stdc++.h>
#include <vector>
#include <queue>

using namespace std;

void prodji(vector<char> matrix[], char S, int n, int m, vector<int> calc[]) {
    int rows = n;
    int cols = m;
    vector<pair<int, int>> pravci = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    queue<pair<int, int>> q;
    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c) {
            if (matrix[r][c] == S) {
                q.push({r, c});
            }
        }
    }
    while (!q.empty()) {
        auto [r, c] = q.front();
        q.pop();
        for (auto [dr, dc] : pravci) {
            int nr = r + dr, nc = c + dc;
            if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && matrix[nr][nc] != '*' && matrix[nr][nc] != S) {
                matrix[nr][nc] = S;
                if(calc[nr][nc] > calc[nr-dr][nc-dc]+1)
                    calc[nr][nc] = calc[nr-dr][nc-dc]+1;
                q.push({nr, nc});
            }
        }
    }
}
int main() {
    int n, m, b = 0;
    cin >> n >> m;
    vector<char> matrix[1010];
    vector<char> matrix2[1010];
    vector<int> calc[1010];
    vector<int> calc2[1010];
     for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            char a;
            cin >> a;
            matrix[i].push_back(a);
            matrix2[i].push_back(a);
            calc[i].push_back(0);
            calc2[i].push_back(0);
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(matrix[i][j] == '.'){
                calc[i][j] = 1000100;
                calc2[i][j] = 1000099;
            }
            if(matrix[i][j] == '*' || matrix[i][j] == 'S')
                calc[i][j] = 0;
            if(matrix[i][j] == '*' || matrix[i][j] == 'X')
                calc2[i][j] = 0;
            if(matrix[i][j] == 'X')
                calc[i][j] = 1000100;
            if(matrix[i][j] == 'S')
                calc2[i][j] = 1000099;
        }
    }
    char S = 'S';
    char X = 'X';
    prodji(matrix, S, n, m, calc);
    prodji(matrix2, X, n, m, calc2);
    /*for (const auto& row : matrix) {
        for (char cell : row) {
            cout << cell << " ";
        }
        cout << endl;
    }*/
    /*for (const auto& row : calc2) {
        for (int cell : row) {
            cout << cell << " ";
        }
        cout << endl;
    }*/
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(calc[i][j] < calc2[i][j])
                b++;
        }
    }
    cout << b;
    return 0;
}