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

int n, m;
char niz[1000][1000];
char ispis[1000][1000];

void pretraga(int is, int js, int ix, int jx) {
    ispis[is][js] = 'S';
    ispis[ix][jx] = 'X';
    queue <pair<int, int> > steva;
    queue <pair<int, int> > vutra;

    steva.emplace(is, js);
    vutra.emplace(ix, jx);
    while (!steva.empty()) {
        is = steva.front().first;
        js = steva.front().second;
        steva.pop();
        if (!vutra.empty()) {
            ix = vutra.front().first;
            jx = vutra.front().second;
            vutra.pop();
        }
        if (is-1 >= 0 and is-1 < n-1 and js >= 0 and js < m-1 and niz[is - 1][js] != '*' and niz[is - 1][js] != 'X') {
            ispis[is - 1][js] = 'S';
            steva.emplace(is - 1, js);
        }
        if (is >= 0 and is < n - 1 and js+1 >= 0 and js+1 < m - 1 and niz[is][js + 1] != '*' and niz[is][js + 1] != 'X') {
            ispis[is][js + 1] = 'S';
            steva.emplace(is, js + 1);
        }
        if (is+1 >= 0 and is+1 < n - 1 and js >= 0 and js < m - 1 and niz[is + 1][js] != '*' and niz[is + 1][js] != 'X') {
            ispis[is + 1][js] = 'S';
            steva.emplace(is + 1, js);
        }
        if (is >= 0 and is < n - 1 and js-1 >= 0 and js-1 < m - 1 and niz[is][js - 1] != '*' and niz[is][js - 1] != 'X') {
            ispis[is][js - 1] = 'S';
            steva.emplace(is, js - 1);
        }

        if (ix-1 >= 0 and ix-1 < n - 1 and jx >= 0 and jx < m - 1 and niz[ix - 1][jx] != '*') {
            ispis[ix - 1][jx] = 'X';
            vutra.emplace(ix - 1, jx);
        }
        if (ix >= 0 and ix < n - 1 and jx+1 >= 0 and jx+1 < m - 1 and niz[ix][jx + 1] != '*') {
            ispis[ix][jx + 1] = 'X';
            vutra.emplace(ix, jx + 1);
        }
        if (ix+1 >= 0 and ix+1 < n - 1 and jx >= 0 and jx < m - 1 and niz[ix + 1][jx] != '*') {
            ispis[ix + 1][jx] = 'X';
            vutra.emplace(ix + 1, jx);
        }
        if (ix >= 0 and ix < n - 1 and jx-1 >= 0 and jx-1 < m - 1 and niz[ix][jx - 1] != '*') {
            ispis[ix][jx - 1] = 'X';
            vutra.emplace(ix, jx - 1);
        }
    }

}

int main()
{
    
    cin >> n >> m;
    
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> niz[i][j];

    int is, js, ix, jx;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (niz[i][j] == 'S') {
                is = i;
                js = j;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (niz[i][j] == 'X') {
                ix = i;
                jx = j;
            }
        }
    }
    pretraga(is, js, ix, jx);

    int brojac = 0;
    for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << ispis[i][j]; cout << '\n'; }
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (ispis[i][j] == 'S') brojac++;
    cout << brojac;
return 0;
}