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

int main()
{
    
    int n, m;
    cin >> n >> m;
    vector<vector<char>> matrica(n, vector<char>(m));
    vector<vector<char>> tempo(n, vector<char>(m));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> matrica[i][j];
            tempo[i][j] = matrica[i][j];
        }
    }
    int br = 0; bool b = true;
    while (b) {
        b = false;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrica[i][j] == 'X') {
                    if (i + 1 < n && tempo[i + 1][j] == '.') {
                        tempo[i + 1][j] = 'X'; b = true;
                    }
                    if (j + 1 < m && tempo[i][j + 1] == '.') {
                        tempo[i][j + 1] = 'X'; b = true;
                    }
                    if (i - 1 >= 0 && tempo[i - 1][j] == '.') {
                        tempo[i - 1][j] = 'X'; b = true;
                    }
                    if (j - 1 >= 0 && tempo[i][j - 1] == '.') {
                        tempo[i][j - 1] = 'X'; b = true;
                    }
                }
            }
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrica[i][j] == 'S') {

                    if (i + 1 < n && tempo[i + 1][j] == '.') {
                        tempo[i + 1][j] = 'S'; b = true;
                    }

                    if (j + 1 < m && tempo[i][j + 1] == '.') {
                        tempo[i][j + 1] = 'S'; b = true;
                    }

                    if (i - 1 >= 0 && tempo[i - 1][j] == '.') {
                        tempo[i - 1][j] = 'S'; b = true;
                    }

                    if (j - 1 >= 0 && tempo[i][j - 1] == '.') {
                        tempo[i][j - 1] = 'S'; b = true;
                    }
                }
            }
            if (b) {
                matrica = tempo;
            }
        }
        
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << matrica[i][j];
            if (matrica[i][j] == 'S') {
                br++;
            }
        }
        cout << endl;
    }
    cout << br;
}