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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <bits/stdc++.h>
using namespace std;

int Board[25][25];

int getBoardSum(int n){
    int sum = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            sum += Board[i][j];
        }
    }
    return sum;
}

int getCollBoard(int n){
    int sum = 0;
    for(int i=0; i<2; i++){
        for(int j=1; j<n; j++){
            sum += Board[i][j];
        }
    }
    return sum;
}

struct Farmer{
    int r=0, c=0;
    int plecak=0;
    int rowToCollect=0;
    bool target=false;
};

void solve(){
    int n; scanf("%d", &n);
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            scanf("%d", &Board[i][j]);
        }
    }
    
    int count = 0;
    int licz = 120;
    
    vector<Farmer> V; 
    int nextRow = n-1, newRow = 0, numFarmer = 0;
    int zloto = 200; bool spot = true, krucho = false, base = true;
    int sumBoard = getBoardSum(n), collectorBoard = getCollBoard(n);
    while(true){
        count++;
        if(sumBoard <= 0){
            bool flag = true;
            if(V[0].plecak != 0) flag = false;
            if(V[1].plecak != 0) flag = false;
            for(int i=2; i<V.size(); i++){
                if(V[i].c != 0) flag = false;
            }
            if(flag) break;
        }
        
        if(zloto >= 100 && numFarmer < 2 && base && spot){
            printf("R FARMER\n"); numFarmer++; zloto -= 100;
            Farmer F; F.rowToCollect = newRow; newRow++; base = false; spot = false;
            V.push_back(F);
        }
        if(zloto >= 100 && 2 <= numFarmer && numFarmer < n && base){
            printf("R FARMER\n"); numFarmer++; zloto -= 100;
            Farmer F; F.rowToCollect = nextRow; nextRow--; base = false;
            V.push_back(F);
        }
        if(collectorBoard == 0 && (zloto+V[0].plecak+V[1].plecak) < 100){
            krucho = true;
        }
        
        for(int i=0; i<V.size(); i++){
            if(V[i].rowToCollect == 0){
                if(V[i].r == 0 && V[i].c == 0){
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c+1);
                    V[i].c++;
                } else if(V[i].r == 0 && V[i].c == 1){
                    if(Board[V[i].r][V[i].c] != 0){
                    } else if(!V[i].target){
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c+1);
                        V[i].c++; spot = true; base = true;
                    } else {
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                        V[i].c--;
                        zloto += V[i].plecak;
                        V[i].plecak = 0; V[i].target = false;
                    }
                } else {
                    if(!V[i].target){
                        if(V[i].plecak >= 100 && numFarmer < n){
                            V[i].target = true;
                        } else if(Board[V[i].r][V[i].c] == 0 && V[i].c == n-1) {
                            V[i].target = true;
                        } else {
                            if(Board[V[i].r][V[i].c] == 0 && V[i].c != n-1){
                                printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c+1);
                                V[i].c++;
                            }
                        }
                    } else {
                        if(V[i].r == 0 && V[i].c == 2){
                            if(spot && base){
                                printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                                V[i].c--; spot = false; base = false;
                            }
                        } else {
                            printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                            V[i].c--;
                        }
                    }
                }
            } else if(V[i].rowToCollect == 1){
                if(V[i].r == 0 && V[i].c == 0){
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c+1);
                    V[i].c++; spot = false;
                } else if(V[i].r == 0 && V[i].c == 1){
                    if(!V[i].target){
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r+1, V[i].c);
                        V[i].r++; spot = true; base = true;
                    } else {
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                        V[i].c--;
                        zloto += V[i].plecak;
                        V[i].plecak = 0; V[i].target = false;
                    }
                } else {
                    if(!V[i].target){
                        if(V[i].plecak >= 100 && numFarmer < n){
                            V[i].target = true;
                        } else if(Board[V[i].r][V[i].c] == 0 && V[i].c == n-1) {
                            V[i].target = true;
                        } else {
                            if(Board[V[i].r][V[i].c] == 0 && V[i].c != n-1){
                                printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c+1);
                                V[i].c++;
                            }
                        }
                    } else {
                        if(V[i].r == 1 && V[i].c == 1){
                            if(spot && base){
                                printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r-1, V[i].c);
                                V[i].r--; spot = false; base = false;
                            }
                        } else {
                            printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                            V[i].c--;
                        }
                    }
                }
            } else if(krucho && nextRow > 1 && V[i].rowToCollect == nextRow+1 && V[i].target == true && V[i].c == 0){
                if(V[i].r == 0){
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r+1, V[i].c);
                    V[i].r++;
                    zloto += V[i].plecak; V[i].plecak = 0; V[i].target = false; krucho = false; base = true;
                } else if(V[i].r == 1){
                    if(base){
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r-1, V[i].c);
                        V[i].r--; base = false;
                    }
                } else {
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r-1, V[i].c);
                    V[i].r--;
                }
            } else {
                if(V[i].rowToCollect == 2 && V[i].r == 1 && V[i].c == 0 && Board[V[i].r][V[i].c] != 0){
                } else if(!V[i].target){
                    if(V[i].r != V[i].rowToCollect){
                        if(V[i].r == 0) base = true;
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r+1, V[i].c);
                        V[i].r++;
                    } else {
                        if(V[i].c != n-1){
                            printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c+1);
                            V[i].c++;
                        } else {
                            V[i].target = true;
                        }
                    }
                } else {
                    if(Board[V[i].r][V[i].c] == 0){
                        if(V[i].c != 0){
                            printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                            V[i].c--;
                        }
                    }
                }
            }
            
            int newZl = min(10, Board[V[i].r][V[i].c]);
            V[i].plecak += newZl;
            Board[V[i].r][V[i].c] -= newZl;
            sumBoard -= newZl;
            if(V[i].rowToCollect == 0 || V[i].rowToCollect == 1){
                collectorBoard -= newZl;
            }
        }
        
        printf("=\n");
    }
    
    
//     printf("\n\n");
//     for(int i=0; i<n; i++){
//         for(int j=0; j<n; j++){
//             printf("%d ", Board[i][j]);
//         }printf("\n");
//     }printf("\n\n");
//     for(int i=0; i<V.size(); i++){
//         printf("%d %d\n", V[i].r, V[i].c);
//     }printf("\n\n%d\n\n", count);
//     
    
    for(int i=0; i<n-1; i++){
        if(V[0].c < n-1){
            printf("M %d %d %d %d\n", V[0].r, V[0].c, V[0].r, V[0].c+1);
            V[0].c++;
        }
        if(V[1].c < n-2){
            printf("M %d %d %d %d\n", V[1].r, V[1].c, V[1].r, V[1].c+1);
            V[1].c++;
        }
        for(int j=V.size()-1; j>1; j--){
            if(V[j].r != 0){
                printf("M %d %d %d %d\n", V[j].r, V[j].c, V[j].r-1, V[j].c);
                V[j].r--;
            } else {
                printf("M %d %d %d %d\n", V[j].r, V[j].c, V[j].r, V[j].c+1);
                V[j].c++;
            }
        }
        printf("=\n");
    }
    
    printf("===\n");
    
/*    
    while(farmHome < n){
        for(int i=0; i<2; i++){
            if(V[i].rowToCollect == 0){
                if(V[i].r == 0 && V[i].c == 0){
                } else if(V[i].r == 0 && V[i].c == 1){
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                    V[i].c--; spot = true; farmHome++;
                } else if(V[i].r == 0 && V[i].c == 2) {
                    if(spot){
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                        V[i].c--; spot = false;
                    }
                } else {
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                    V[i].c--;
                }
            } else {
                if(V[i].r == 0 && V[i].c == 0){
                } else if(V[i].r == 0 && V[i].c == 1){
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                    V[i].c--; spot = true; farmHome++;
                } else if(V[i].r == 1 && V[i].c == 1) {
                    if(spot){
                        printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r-1, V[i].c);
                        V[i].r--; spot = false;
                    }
                } else {
                    printf("M %d %d %d %d\n", V[i].r, V[i].c, V[i].r, V[i].c-1);
                    V[i].c--;
                }
            }
        }
    }*/
}

int main(){
    int t, k; scanf("%d%d", &t, &k);
    while(t--){
        solve();
    }
}