/*
 * Created by SharpDevelop.
 * User: Home
 * Date: 11/17/2024
 * Time: 10:03 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;

namespace Lavirint
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.Write("Unesite dimenzije lavirinta N i M redom (razdvojite razmakom): ");
			string[] niz = Console.ReadLine().Split();
			int N = int.Parse(niz[0]);
			int M = int.Parse(niz[1]);
			
			Console.WriteLine("U daljem toku zadatka, koordinate krecu od 0,0!");
			
			Console.Write("Unesite tacku gde se nalazi Steva na pocetku: ");
			string[] niz1 = Console.ReadLine().Split();
			int xS = int.Parse(niz1[0]);
			int yS = int.Parse(niz1[1]);
			
			Console.Write("Koliko ima polja gde su zidovi i gde je vatra na pocetku? ");
			string[] niz2 = Console.ReadLine().Split();
			int nZ = int.Parse(niz2[0]);
			int nV = int.Parse(niz2[1]);
			
			//var lista_xZ = new List<int>();
			//var lista_yZ = new List<int>();
			var Zidovi = new Dictionary<int, List<int>>();
			Console.WriteLine("U narednih " + nZ + " redova unosite koordinate polja gde se nalaze zidovi: ");
			for(int i=0; i<nZ; i++)
			{
				string[] niz3 = Console.ReadLine().Split();
				int xZ = int.Parse(niz3[0]);
				int yZ = int.Parse(niz3[1]);
				//lista_xZ.Add(xZ);
				//lista_yZ.Add(yZ);
				Zidovi.Add(i, new List<int>(){xZ, yZ});
			}
			
			//var lista_xV = new List<int>();
			//var lista_yV = new List<int>();
			var Vatre = new Dictionary<int, List<int>>();
			Console.WriteLine("U narednih " + nV + " redova unosite koordinate polja gde se nalazi vatra: ");
			for(int i=0; i<nV; i++)
			{
				string[] niz4 = Console.ReadLine().Split();
				int xV = int.Parse(niz4[0]);
				int yV = int.Parse(niz4[1]);
				//lista_xV.Add(xV);
				//lista_yV.Add(yV);
				Vatre.Add(i, new List<int>(){xV, yV});
			}
			
			char[,] matrica = new char[N, M];
			
			for(int i=0; i<N; i++)
			{
				for(int j=0; j<M; j++)
				{
					matrica[i, j] = '.';
					
					if(i == xS && j == yS)
					{
						matrica[i, j] = 'S';
					}
					
					for(int k=0; k<nV; k++)
					{
						if(i == Vatre[k][0] && j == Vatre[k][1])
						{
							matrica[i, j] = 'X';
						}
					}
					
					for(int g=0; g<nZ; g++)
					{
						if(i == Zidovi[g][0] && j == Zidovi[g][1])
						{
							matrica[i, j] = '*';
						}
					}
				}
			}
			
			for(int i=0; i<N; i++)
			{
				for(int j=0; j<M; j++)
				{
					Console.Write(matrica[i, j]);
				}
				Console.WriteLine();
			}
			
			//bool ima_gde = true;
			int br = 1;
			int ix = 1, iy = 1;
			for(int i=0; i<2; i++)
			{
				if(xS-ix >= 0)
				{
					if(matrica[xS-ix, yS] != '*' && matrica[xS-ix, yS] != 'X')
					{
						matrica[xS-ix, yS] = 'S';
						br++;
					}
				}
				if(yS-iy >= 0)
				{
					if(matrica[xS, yS-iy] != '*' && matrica[xS, yS-iy] != 'X')
					{
						matrica[xS, yS-iy] = 'S';
						br++;
					}
				}
				if(xS+ix < N)
				{
					if(matrica[xS+ix, yS] != '*' && matrica[xS+ix, yS] != 'X')
					{
						matrica[xS+ix, yS] = 'S';
						br++;
					}
				}
				if(xS+iy < M)
				{
					if(matrica[xS, yS+iy] != '*' && matrica[xS, yS+iy] != 'X')
					{
						matrica[xS, yS+iy] = 'S';
						br++;
					}
				}
				
				for(int k=0; k<nV; k++)
				{
					if(Vatre[k][0]-ix >= 0)
					{
						if(matrica[Vatre[k][0]-ix, Vatre[k][1]] != '*')
						{
							if(matrica[Vatre[k][0]-ix, Vatre[k][1]] == 'S')
							{
								br--;
							}
							matrica[Vatre[k][0]-ix, Vatre[k][1]] = 'X';
						}
					}
					
					if(Vatre[k][1]-iy >= 0)
					{
						if(matrica[Vatre[k][0], Vatre[k][1]-iy] != '*')
						{
							if(matrica[Vatre[k][0], Vatre[k][1]-iy] == 'S')
							{
								br--;
							}
							matrica[Vatre[k][0], Vatre[k][1]-iy] = 'X';
						}
					}
					if(Vatre[k][0]+ix < N)
					{	
						if(matrica[Vatre[k][0]+ix, Vatre[k][1]] != '*')
						{
							if(matrica[Vatre[k][0]+ix, Vatre[k][1]] == 'S')
							{
								br--;
							}
							matrica[Vatre[k][0]+ix, Vatre[k][1]] = 'X';
						}
					}
					if(Vatre[k][1]+iy < M)
					{
						if(matrica[Vatre[k][0], Vatre[k][1]+iy] != '*')
						{
							if(matrica[Vatre[k][0], Vatre[k][1]+iy] == 'S')
							{
								br--;
							}
							matrica[Vatre[k][0], Vatre[k][1]+iy] = 'X';
						}
					}
				}
				
				ix++;
				iy++;
				
				/*int brS = 1;
				for(int i=0; i<N; i++)
				{
					for(int j=0; j<M; j++)
					{
						if(matrica[i, j] == 'S')
						{
							brS++;
						}
					}
				}
				if(br == 0 || brS == N*M)
					ima_gde = false;*/
			}
			for(int i=0; i<N; i++)
			{
				for(int j=0; j<M; j++)
				{
					Console.Write(matrica[i, j]);
				}
				Console.WriteLine();
			}
			
			// TODO: Implement Functionality Here
			
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
	}
}