import java.io.*;
import java.util.*;
public class T2Q1
{
	public static MineSweeper ms;
	public static void main(String[]args) throws IOException
	{
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		init();
		while(true)
		{
			
			System.out.println(ms);
			System.out.println("Mine Counter: " + ms.getMineCounter());
			String s = stdin.readLine();
			
			try
			{			
				//Possible Commands "L|R X Y
				StringTokenizer st = new StringTokenizer(s);
				String command = st.nextToken();
				int X = Integer.parseInt(st.nextToken());
				int Y = Integer.parseInt(st.nextToken());
				
				if(command.toUpperCase().equals("L"))
					leftClickEvent(X, Y);
				if(command.toUpperCase().equals("R"))
					rightClickEvent(X, Y);				
			}
			catch(Exception e)
			{
				continue;
			}
			
			if(ms.hasLost())
			{
				System.out.println(ms);
				System.out.println("You have uncovered a mine! You lose!");
				break;
			}
			
		}
	}
	
	public static void init()
	{
		ms = new MineSweeper(9,9);
	}
	
	public static void leftClickEvent(int x, int y)
	{
		ms.uncover(x,y);
	}
	
	public static void rightClickEvent(int x, int y)
	{
		ms.toggleFlag(x,y);
	}
}

class MineSweeper
{
	private Tile[][]grid;  
	private int mineCounter; 

	//constructor. Sets the size of the grid
	public MineSweeper(int x, int y)
	{
		grid = new Tile[x][y];
		for(int i = 0; i < x; i++)
		{
			for(int j = 0; j < y; j++)
			{
				double k = Math.random() * 100;
				if(k < 20)
					grid[i][j] = new Tile(true);
				else
					grid[i][j] = new Tile(false);
			}
		}
		
		mineCounter = this.getNumMines();
	}

	//---- For gameplay use ---
	//Return true if the game is won
	public boolean hasWon()
	{
		return true;
	}
	
	//Returns true if the game is lost
	public boolean hasLost()
	{
		//If for all tiles, an uncovered tile is a mine, then game is lost
		for(int i = 0; i < grid.length; i++)
		{
			for(int j = 0; j < grid[0].length; j++)
			{
				if(grid[i][j].isUncovered() && grid[i][j].isMine())
					return true;
			}
		}
		
		return false;
	}
	
	//Uncover a tile (left click)
	public void uncover(int x, int y) 
	{
		grid[y][x].uncover();
	}
	
	//Increment the flag status on a tile. This is done when 
	//the user wants to toggle flags on a mine. Returns the new 	
	//flag status after toggled. 
	public int toggleFlag(int x, int y) 
	{
		int flag = grid[y][x].toggleFlag();
		if(flag == 1)
			mineCounter--;
		if(flag == 2)
			mineCounter++;
			
		return flag;
	}
	
	public boolean isUncovered(int x, int y)
	{
		return grid[y][x].isUncovered();
	}
		
	
	public int getMineCounter()
	{
		return mineCounter;
	}
	
	//Get the number of mines on a tile's neighbors
	public int getNeighborMines(int x, int y) 
	{
		int mineCount = 0;
		for(int i=x - 1; i <= x+1; i++)
		{
			//If outside of boundary, skip
			if(i < 0 || i > grid.length)
			{
				continue;
			}
			for(int j = y - 1; j <= y+1; j++)
			{
				if(j < 0 || j > grid[0].length)
				{
					continue;
				}
				if(grid[i][j].isMine())
				{
					mineCount++;
				}
			}
		}
		return mineCount;
	}
	
	public int getNumMines()
	{
		int mineCount = 0;
		for(int i = 0; i < grid.length; i++)
		{
			for(int j = 0; j < grid[0].length; j++)
			{
				if(grid[i][j].isMine())	
				{
					mineCount++;
				}
			}
		}
		return mineCount;
	}
	
	public String toString()
	{
		String returnString = "";
		for(int i = grid.length - 1; i >= 0; i--)
		{	
			returnString += i + " ";
			for(int j = 0; j < grid[0].length; j++)
			{
				if(!grid[i][j].isUncovered())			
					returnString += grid[i][j].getTileString() + " ";
				else if(grid[i][j].isUncovered() && !grid[i][j].isMine())
					returnString += this.getNeighborMines(i,j) + " ";
				else if(grid[i][j].isUncovered() && grid[i][j].isMine())
					returnString += grid[i][j].getTileString() + " ";
			}
			
			returnString += "\n";
		}
		
		returnString += "  ";
		for(int j = 0; j < grid[0].length; j++)
		{
			returnString += j + " "	;
		}
		return returnString;
	}

}

class Tile
{
	//0 = no flag, 1 = mine, 2 = ?
	private int flagStatus; 
	private boolean isUncovered;
	private boolean isMine;
	public Tile(boolean isMine)
	{
		boolean isUncovered = false;
		this.isMine = isMine;
	}

	//Toggles the flag status. Returns the new flag status.
	public int toggleFlag()
	{
		flagStatus++;
		if(flagStatus == 3)
			flagStatus = 0;
		return flagStatus;
	}
	
	//Sets tile to 'Uncovered' or opened status
	public void uncover()
	{
		isUncovered = true;
	}
	
	public boolean isUncovered()
	{
		return isUncovered;
	}
	
	public String getTileString()
	{
		if(!this.isUncovered() && flagStatus == 0)
			return "_";
		else if(!this.isUncovered() && flagStatus == 1)
			return "M";
		else if(!this.isUncovered() && flagStatus == 2)
			return "?";
		else if(this.isUncovered() && this.isMine())
			return "*";
		else
			return " ";			
	}
	
	//Returns true if tile is a mine. (Used for checking lost 	//condition) or neighboring mines.
	public boolean isMine()
	{
		return isMine;
	}

}
