#include<iostream>
#include<string>
using namespace std;
const int MaxVelicina=1000;
void SirenjeVatre(string l[][MaxVelicina], int n, int m)
{
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<m; j++)
		{
			if (l[i][j] == "X")
			{
                if (i>0 and l[i-1][j]!="*")
                l[i-1][j]="X";
                if (i<n-1 and l[i+1][j]!="*")
                l[i+1][j]="X";
                if (j>0 and l[i][j-1] !="*")
                l[i][j-1]="X";
                if (j<m-1 and l[i][j+1]!="*")
                l[i][j+1]="X";
            }
		}
	}
}
int main()
{
	int i, j, n, m;
	cin >> n >> m;
	string l[MaxVelicina][MaxVelicina];
	for(i=0; i<n; i++)
	{
		for(j=0; j<m; j++)
		{
			cin >> l[i][j];
		}
	}
	int br=0;
	SirenjeVatre(l, n, m);
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<m; j++)
		{
			if (l[i][j]=="S")
			{
                if (i>0 and l[i-1][j]==".")
                {
                	l[i-1][j]="S";
                	br++;
                	SirenjeVatre(l, n, m);
				}
                if (i<n-1 and l[i+1][j]==".")
                {
                	l[i+1][j]="S";
                	br++;
                	SirenjeVatre(l, n, m);
				}
                if (j>0 and l[i][j-1]==".")
                {
                	l[i][j-1]="S";
                	br++;
                	SirenjeVatre(l, n, m);
				}
                if (j<m-1 and l[i][j+1]==".")
                {
                	l[i][j+1]="S";
                	br++;
                	SirenjeVatre(l, n, m);
				}
            }
		}
	}
	for(i=0; i<n; i++)
	{
		for(j=0; j<m; j++)
		{
			cout << l[i][j];
		}
		cout << endl;
	}
	cout << br;
	return 0;
}