#include "manager.h"
#include <iostream>
#include <algorithm>

const std::string PASSWORD = "egoi2026fans";
const int PROCESSES = 2;
const int MAXL = 51;

int games, n, max_length = MAXL, solved[MAXL + 1], total[MAXL + 1];
char** fds;

void finish(){
    for (int i = 0; i < 2 * PROCESSES; i++){
        close(atoi(fds[i]));
    }
    exit(0);
}

enum Player { Azzurro = 0, Bordeaux = 1 };
enum ErrorMessage { CONTESTANT_STDOUT = -1, BAD_TABLE = -2, BAD_STRING = -3 };

void my_assert(bool condition, const ErrorMessage& error_message){
	if(condition) return;
	std::cout << error_message << std::endl;
	exit(0);
}

void play_game(){
	int length; std::cin >> length;
	std::string str; std::cin >> str;
	std::string moves; std::cin >> moves;
	
	// giving str to Azzurro, he will encode it into
	// n-by-n table of 'A's and 'B's
	out(Azzurro) << length << endl;
	out(Azzurro) << str << endl;
	
	// get Azzurro's encoding
	{
		std::string pass;
		in(Azzurro) >> pass;
		my_assert(pass == PASSWORD, CONTESTANT_STDOUT);
	}
	std::vector<std::vector<int>> encoded(n, std::vector<int>(n, 0));
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < n; j ++){
			in(Azzurro) >> encoded[i][j];
		}
	}
	
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < n; j ++){
			my_assert(encoded[i][j] == 0 || encoded[i][j] == 1, BAD_TABLE);
		}
	}
	
	// grader induces error in the message
	for(int i = 0, x = 0, y = 0; i < moves.size(); i ++){
		encoded[x][y] ^= 1;
		if(moves[i] == 'R') ++ y;
		else				++ x;
	}
	encoded[n - 1][n - 1] ^= 1;
	
	// pass Azzurro's encoding to Bordeaux to decode it
	out(Bordeaux) << length << endl;
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < n; j ++){
			out(Bordeaux) << encoded[i][j] << " ";
		}
		out(Bordeaux) << endl;
	}
	
	{
		std::string pass;
		in(Bordeaux) >> pass;
		my_assert(pass == PASSWORD, CONTESTANT_STDOUT);
	}
	// get Bordeaux's attempt at decoding
	std::string decoded;
	in(Bordeaux) >> decoded;
	decoded.pop_back();
	// remove the last dummy character
	
	my_assert(decoded.size() == str.size(), BAD_STRING);
	for(int i = 0; i < decoded.size(); i ++){
		my_assert(decoded[i] == 'A' || decoded[i] == 'B', BAD_STRING);
	}
	
	if(decoded != str){
		max_length = min(max_length, length - 1);
	}
}

int main(int argc, char** argv){
	fds = argv + 1;
	fd_to_stream(fds, 2 * PROCESSES);
	
	out(Azzurro) << static_cast<bool>(Azzurro) << endl;
	out(Bordeaux) << static_cast<bool>(Bordeaux) << endl;

	std::cin >> games;
	std::cin >> n;
	
	out(Azzurro) << games << " " << n << endl;
	out(Bordeaux) << games << " " << n << endl;
	
	for(int i = 0; i < games; i ++){
		play_game();
	}
	
	std::cout << max_length << std::endl;
	finish();
	return 0;
}
