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

char mat[1000][1000];

vector <pair <int, int>> PronadjiX(int N, int M, char mat[][1000]) {
	vector <pair<int, int>> v1;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (mat[i][j] == 'X') {
				
				v1.push_back(make_pair(i, j));
			}
		}
	}
	return v1;
}
vector<pair <int, int>> PronadjiS(int N, int M, char mat[][1000]) {
	vector <pair<int, int>> v1;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (mat[i][j] == 'S') {
				v1.push_back(make_pair(i, j));
			}
		}
	}
	return v1;
}
int KorakX(int n, int m, char mat[][1000], int x1, int y1) {
	int br = 0;

	if (mat[x1+1][y1] == '.' && x1 + 1 < n)
	{
		mat[x1 + 1][y1] = 'X';
		br++;
	}

	if (mat[x1 - 1][y1] == '.'&& x1 - 1 >= 0)
	{
		mat[x1 - 1][y1] = 'X';
		br++;
	}

	if (mat[x1][y1 + 1] == '.' && y1 + 1 < m)
	{
		mat[x1][y1 + 1] = 'X';
		br++;
	}

	if (mat[x1][y1 - 1] == '.'&& y1 - 1 >= 0)
	{
		mat[x1][y1 - 1] = 'X';
		br++;
	}
	return br;
}
	
int KorakS(int n, int m, char mat[][1000], int x1, int y1) {
	int br = 0;
	if (mat[x1 + 1][y1] == '.' && x1 + 1 < n)
	{
		mat[x1 + 1][y1] = 'S';
		br++;
	}

	if ( mat[x1 - 1][y1] == '.' && x1 - 1 >= 0 )
	{
		mat[x1 - 1][y1] = 'S';
		br++;
	}

	if (mat[x1][y1 + 1] == '.' && y1 + 1 < m)
	{
		mat[x1][y1 + 1] = 'S';
		br++;
	}

	if (mat[x1][y1 - 1] == '.'&& y1 - 1 >= 0)
	{
		mat[x1][y1 - 1] = 'S';
		br++;
	}
	return br;
}
int PrebrojS(int n, int m, char mat[][1000]) {
	int br = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (mat[i][j] == 'S') {
				br++;
			}
		}
	}
	return br;
}
int main()
{
	int n, m;
	cin >> n;
	cin >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
		}
	}
	while (true) {
		vector < pair < int, int >> p1 = PronadjiX(n, m, mat);
		vector < pair < int, int >> p2 = PronadjiS(n, m, mat);
		int br1max = 0;
		int br2max = 0;
		if (!p1.empty())
		{
			for (int i = 0; i < p1.size(); i++) {
				int br1 = KorakX(n, m, mat, p1[i].first, p1[i].second);
				if (br1 > br1max) {
					br1max = br1;
				}
			}
		}
		if (!p2.empty())
		{
			for (int i = 0; i < p2.size(); i++) {
				int br2 = KorakS(n, m, mat, p2[i].first, p2[i].second);
				if (br2 > br2max) {
					br2max = br2;
				}
			}
		}
		if (br1max == 0 && br2max == 0) {
			break;
		}
	}
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << mat[i][j]<<" ";
		}
		cout << endl;
	}
	int a = PrebrojS(n, m, mat);
	cout << a;
}