1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include<iostream>
//#include<windows.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<bitset>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector< ii > vii;
typedef vector< pair < ii, int > > viii;
typedef vector< vector < ii > > vvii;
typedef pair < pair < int, int >, int >  iii;
typedef pair < pair < int, int >, pair < int, int > > iiii;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef vector< ll > vll;
typedef long double ld;
typedef map < int, int > MAPA;

#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define st first
#define nd second
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rall(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b)    for (int (i)=(a); (i)<(b); (i)++)
#define FOR2(i, a, b)    for (int (i)=(a); (i)>=(b); (i)--)

template<typename T> inline void setmin(T &x, T y) { if (y < x) x = y; }
template<typename T> inline void setmax(T &x, T y) { if (y > x) x = y; }
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

const int MAX = 2e6 + 7;
const int T = 1 << 20;
const int INF = 1e9 + 7;
const ll BIG_INF = 1e18 + 5;

ld e = 2.7182818284590452353602874713526624;
ld PI = acos(-1);
ld eps = 1e-19;

ll n, m, k, t;
ll current_gold;

int tab[20][20];
int w_plecaku[20][20];
int odw[20][20];
int akt_zakr;
int farmerzy;

int losuj(int a, int b) {
	return a + rand() % (b - a + 1);
}

void ustaw_poczatkowy_stan() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> tab[i][j];
			//if( i || j )tab[i][j] = pow(2, losuj(0, 9));
			w_plecaku[i][j] = 0;
			odw[i][j] = 0;
		}
	}
	farmerzy = 0;
	current_gold = 200;
	akt_zakr = 1;
}

bool jest_jeszcze() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (w_plecaku[i][j] || tab[i][j]) {
				return 1;
			}
		}
	}
	return 0;
}

bool nie_ma_do() {
	for (int i = 0; i <= akt_zakr; i++) {
		for (int j = 0; j < n; j++) {
			if (w_plecaku[i][j] || tab[i][j]) {
				return 0;
			}
		}
	}
	return 1;
}

void wykonaj_ruch() {
	bool byl = 0;
	if ( odw[0][0] ) {
		byl = 1;
	}
	for (int i = 1; i <= akt_zakr; i++) {
		if (!odw[i - 1][0] && odw[i][0]) {
			cout << "M " << i << ' ' << 0 << ' ' << i - 1 << ' ' << 0 << '\n';
			odw[i - 1][0] = 1;
			odw[i][0] = 0;
			w_plecaku[i - 1][0] = w_plecaku[i][0];
			w_plecaku[i][0] = 0;
		}
	}
	for (int i = 1; i <= akt_zakr; i++) {
		for (int j = 1; j < n; j++) {
			if (j == n - 1 && i != akt_zakr) {
				continue;
			}
			if (!odw[i][j - 1] && odw[i][j]) {
				cout << "M " << i << ' ' << j << ' ' << i << ' ' << j - 1 << '\n';
				odw[i][j - 1] = 1;
				odw[i][j] = 0;
				w_plecaku[i][j - 1] = w_plecaku[i][j];
				w_plecaku[i][j] = 0;
			}
		}
	}
	for (int i = akt_zakr - 1; i >= 0; i--) {
		if (!odw[i + 1][n - 1] && odw[i][n - 1]) {
			cout << "M " << i << ' ' << n-1 << ' ' << i + 1 << ' ' << n-1 << '\n';
			odw[i + 1][n - 1] = 1;
			odw[i][n - 1] = 0;
			w_plecaku[i + 1][n - 1] = w_plecaku[i][n - 1];
			w_plecaku[i][n - 1] = 0;
		}
	}
	for (int j = n - 2; j > 0; j--) {
		if (odw[0][j] && !odw[0][j + 1]) {
			cout << "M " << 0 << ' ' << j << ' ' << 0 << ' ' << j + 1 << '\n';
			odw[0][j] = 0;
			odw[0][j + 1] = 1;
			w_plecaku[0][j + 1] = w_plecaku[0][j];
			w_plecaku[0][j] = 0;
		}
	}

	if (byl) {
		cout << "M " << 0 << ' ' << 0 << ' ' << 0 << ' ' << 1 << '\n';;
		odw[0][1] = 1;
		odw[0][0] = 0;
	}
}

void kup_farmera() {
	farmerzy++;
	current_gold -= 100;
	odw[0][1] = 1;
	cout << "R FARMER\n" << "M 0 0 0 1\n";
}

void zakoncz_ture() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (odw[i][j]) {
				if (i == 0 && j == 0) {
					current_gold += w_plecaku[0][0];
					w_plecaku[0][0] = 0;
				}
				else {
					int zmiana = min(10, tab[i][j]);
					w_plecaku[i][j] += zmiana;
					tab[i][j] -= zmiana;
				}
			}
		}
	}
	cout << "=\n";
}
void jazda_pokaz() {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cout << odw[i][j] << ' ';
		}
		cout << '\t';
		for (int j = 0; j < n; ++j) {
			cout << tab[i][j] << ' ';
		}
		cout << '\t';
		for (int j = 0; j < n; ++j) {
			cout << w_plecaku[i][j] << ' ';
		}
		cout << '\n';
	}
}
void zakoncz_gre() {
	cout << "===\n";
}

int main() {
	boost;
	srand(time(0));
	cin >> t >> k;

	while (t--) {
		cin >> n;
		int ile_tur = 0;
		ustaw_poczatkowy_stan();
		while (jest_jeszcze()) {
			ile_tur++;
			wykonaj_ruch();
			if (!odw[0][1] && !odw[0][0] && farmerzy < (2 * n - 1) && current_gold >= 100) {
				kup_farmera();
			}
			zakoncz_ture();
			if (nie_ma_do()) {
				akt_zakr++;
			}
			//jazda_pokaz();
		}
		zakoncz_gre();
		//cout << ile_tur << endl;
	}
}