import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


        System.out.println("??????? ???? ?????? N ? ???? ?????? M:");
        int N = sc.nextInt();
        int M = sc.nextInt();


        char[][] lavirint = new char[N][M];

        int iSteve=0,jSteve=0;
        int iVatra=0,jVatra=0;
        System.out.println("??????? ??????? ?? ???????? (Koristi:[. , * , X , S]):");
        for (int i = 0; i < N; i++) {
            String red = sc.next();
            for (int j = 0; j < M; j++) {
                lavirint[i][j] = red.charAt(j);
                if(lavirint[i][j] == 'S'){
                    iSteve = i;jSteve = j;
                }

                if (lavirint[i][j] == 'X'){
                    iVatra = i;jVatra = j;
                }
            }
        }
        int br=1;
for (int i = 0; i < lavirint.length; i++) {
    for (int j = 0; j < lavirint[i].length; j++) {
        System.out.printf(lavirint[i][j] + " ");
    }
    System.out.println();
}

    System.out.println("Steva ima koodrinate:" + "[" + iSteve + "," + jSteve + "]");
    System.out.println("Vatra ima koordinate:" + "[" + iVatra + "," + jVatra + "]");

    for (int i = 0; i < lavirint.length; i++) {
        for (int j = 0; j < lavirint[i].length; j++) {
            if(iSteve-1 >= 0){
                if(lavirint[iSteve-1][jSteve] == '.'){
                    lavirint[iSteve-1][jSteve] = 'S';br++;
                }}
            if(iSteve+1 < lavirint.length){
                if(lavirint[iSteve+1][jSteve] == '.'){
                    lavirint[iSteve+1][jSteve] = 'S';br++;
                }}
            if(jSteve+1 < lavirint[i].length){
                if(lavirint[iSteve][jSteve+1] == '.'){
                    lavirint[iSteve][jSteve+1] = 'S';br++;
                }}
            if(jSteve-1 >= 0){
                if(lavirint[iSteve][jSteve-1] == '.'){
                    lavirint[iSteve][jSteve-1] = 'S';br++;
                }}

            if(iVatra-1 >= 0){
                if(lavirint[iVatra-1][jVatra] == '.'){
                    lavirint[iVatra-1][jVatra] = 'X';
                }}
            if(iVatra+1 < lavirint.length){
                if(lavirint[iVatra+1][jVatra] == '.'){
                    lavirint[iVatra+1][jVatra] = 'X';
                }}
            if(jVatra+1 < lavirint[i].length){
                if(lavirint[iVatra][jVatra+1] == '.'){
                    lavirint[iVatra][jVatra+1] = 'X';
                }}
            if(jVatra-1 >= 0){
                if(lavirint[iVatra][jVatra-1] == '.'){
                    lavirint[iVatra][jVatra-1] = 'X';
                }}
        }
    }

    for (int i = 0; i < lavirint.length; i++) {
        for (int j = 0; j < lavirint[i].length; j++) {
            System.out.printf(lavirint[i][j] + " ");
        }
        System.out.println();
    }

        System.out.println("Broj polja je:" + br);

    }
}