-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHEFPNT.cpp
1031 lines (1031 loc) · 43 KB
/
CHEFPNT.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<bits/stdc++.h>
using namespace std;
int a[60][110][110],ans[60][10100][3];
int main(){int n,m,k,l[60],i,j,q,countj,counti,tmp,tmp1,mini,flag;
scanf("%d %d %d",&n,&m,&k);
int x[k],y[k];
for(i=0;i<=n;i++)for(j=0;j<=m;j++)a[0][i][j]=0;
for(i=0;i<k;i++){scanf("%d %d",&x[i],&y[i]);
a[0][x[i]-1][y[i]-1]=1;}
for(q=1;q<50;q++)for(i=0;i<=n;i++)for(j=0;j<=m;j++)a[q][i][j]=a[0][i][j];
for(q=0;q<50;q++) l[q]=0;
for(i=0;i<n;i++)for(j=0;j<m;j++)if(a[0][i][j]==0){ans[0][l[0]][0]=i+1,ans[0][l[0]][1]=j+1,ans[0][l[0]++][2]=0;
while(a[0][i][j]!=1&&j<m)a[0][i][j++]=1;}
for(i=0;i<n;i++)for(j=0;j<m;j++)if(a[1][i][j]==0){ans[1][l[1]][0]=i+1,ans[1][l[1]][1]=j+1,countj=0,tmp=j;
while(a[1][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[1][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[1][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[1][l[1]++][2]=0;
while(a[1][i][j]!=1&&j<m)a[1][i][j++]=1;}
else{ans[1][l[1]++][2]=1,tmp1=i;
while(a[1][tmp1][j]!=1&&tmp1<n)a[1][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[1][tmp1][j]!=1&&tmp1>=0)a[1][tmp1--][j]=1;}}
for(i=n-1;i>=0;i--)for(j=m-1;j>=0;j--)if(a[2][i][j]==0){ans[2][l[2]][0]=i+1,ans[2][l[2]][1]=j+1,countj=0,tmp=j;
while(a[2][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[2][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[2][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[2][l[2]++][2]=0;
while(a[2][i][j]!=1&&j>=0)a[2][i][j--]=1;}
else{ans[2][l[2]++][2]=1,tmp1=i;
while(a[2][tmp1][j]!=1&&tmp1<n)a[2][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[2][tmp1][j]!=1&&tmp1>=0)a[2][tmp1--][j]=1;}}
for(i=0;i<n;i++)for(j=m-1;j>=0;j--)if(a[3][i][j]==0){ans[3][l[3]][0]=i+1,ans[3][l[3]][1]=j+1;countj=0;
tmp=j;
while(a[3][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[3][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[3][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[3][l[3]++][2]=0;
while(a[3][i][j]!=1&&j>=0)a[3][i][j--]=1;}
else{ans[3][l[3]++][2]=1,tmp1=i;
while(a[3][tmp1][j]!=1&&tmp1<n)a[3][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[3][tmp1][j]!=1&&tmp1>=0)a[3][tmp1++][j]=1;}}
for(i=n-1;i>=0;i--)for(j=0;j<m;j++)if(a[4][i][j]==0){ans[4][l[4]][0]=i+1,ans[4][l[4]][1]=j+1,countj=0,tmp=j;
while(a[4][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[4][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[4][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[4][l[4]++][2]=0;
while(a[4][i][j]!=1&&j<m)a[4][i][j++]=1;}
else{ans[4][l[4]++][2]=1,tmp1=i;
while(a[4][tmp1][j]!=1&&tmp1<n)a[4][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[4][tmp1][j]!=1&&tmp1>=0)a[4][tmp1--][j]=1;}}
for(i=0;i<m;i++)for(j=0;j<n;j++)if(a[5][j][i]==0){ans[5][l[5]][0]=j+1,ans[5][l[5]][1]=i+1;countj=0;
tmp=j;
while(a[5][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[5][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[5][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[5][l[5]++][2]=1;
while(a[5][j][i]!=1&&j<n)a[5][j++][i]=1;}
else{ans[5][l[5]++][2]=0;
tmp1=i;
while(a[5][j][tmp1]!=1&&tmp1<m)a[5][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[5][j][tmp1]!=1&&tmp1>=0)a[5][j][tmp1--]=1;}}
for(i=m-1;i>=0;i--)for(j=n-1;j>=0;j--)if(a[6][j][i]==0){ans[6][l[6]][0]=j+1,ans[6][l[6]][1]=i+1;countj=0;
tmp=j;
while(a[6][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[6][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[6][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[6][l[6]++][2]=1;
while(a[6][j][i]!=1&&j>=0)a[6][j--][i]=1;}
else{ans[6][l[6]++][2]=0;
tmp1=i;
while(a[6][j][tmp1]!=1&&tmp1<m)a[6][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[6][j][tmp1]!=1&&tmp1>=0)a[6][j][tmp1--]=1;}}
for(i=0;i<m;i++)for(j=n-1;j>=0;j--)if(a[7][j][i]==0){ans[7][l[7]][0]=j+1,ans[7][l[7]][1]=i+1;countj=0;
tmp=j;
while(a[7][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[7][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[7][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[7][l[7]++][2]=1;
while(a[7][j][i]!=1&&j>=0)a[7][j--][i]=1;}
else{ans[7][l[7]++][2]=0;
tmp1=i;
while(a[7][j][tmp1]!=1&&tmp1<m)a[7][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[7][j][tmp1]!=1&&tmp1>=0)a[7][j][tmp1--]=1;}}
for(i=m-1;i>=0;i--)for(j=0;j<n;j++)if(a[8][j][i]==0){ans[8][l[8]][0]=j+1,ans[8][l[8]][1]=i+1;countj=0;
tmp=j;
while(a[8][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[8][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[8][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[8][l[8]++][2]=1;
while(a[8][j][i]!=1&&j<n){a[8][j++][i]=1;}}
else{ans[8][l[8]++][2]=0;
tmp1=i;
while(a[8][j][tmp1]!=1&&tmp1<m)a[8][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[8][j][tmp1]!=1&&tmp1>=0)a[8][j][tmp1--]=1;}}
for(j=0;j<m;j++)for(i=0;i<n;i++)if(a[9][i][j]==0){ans[9][l[9]][0]=i+1,ans[9][l[9]][1]=j+1,ans[9][l[9]++][2]=1;
while(a[9][i][j]!=1&&i<n)a[9][i++][j]=1;}
for(i=0;i<n;i++)for(j=0;j<m;j++)if(a[10][i][j]==0){ans[10][l[10]][0]=i+1,ans[10][l[10]][1]=j+1;countj=0;
tmp=j;
while(a[10][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[10][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[10][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[10][l[10]++][2]=0;
while(a[10][i][j]!=1&&j<m)a[10][i][j++]=1;}
else{ans[10][l[10]++][2]=1,tmp1=i;
while(a[10][tmp1][j]!=1&&tmp1<n)a[10][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[10][tmp1][j]!=1&&tmp1>=0)a[10][tmp1][j]=1;}}
for(i=n-1;i>=0;i--)for(j=m-1;j>=0;j--)if(a[11][i][j]==0){ans[11][l[11]][0]=i+1,ans[11][l[11]][1]=j+1;countj=0;
tmp=j;
while(a[11][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[11][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[11][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[11][l[11]++][2]=0;
while(a[11][i][j]!=1&&j>=0)a[11][i][j--]=1;}
else{ans[11][l[11]++][2]=1,tmp1=i;
while(a[11][tmp1][j]!=1&&tmp1<n)a[11][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[11][tmp1][j]!=1&&tmp1>=0)a[11][tmp1--][j]=1;}}
for(i=0;i<n;i++)for(j=m-1;j>=0;j--)if(a[12][i][j]==0){ans[12][l[12]][0]=i+1,ans[12][l[12]][1]=j+1;countj=0;
tmp=j;
while(a[12][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[12][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[12][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[12][l[12]++][2]=0;
while(a[12][i][j]!=1&&j>=0)a[12][i][j--]=1;}
else{ans[12][l[12]++][2]=1,tmp1=i;
while(a[12][tmp1][j]!=1&&tmp1<n)a[12][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[12][tmp1][j]!=1&&tmp1>=0)a[12][tmp1--][j]=1;}}
for(i=n-1;i>=0;i--)for(j=0;j<m;j++)if(a[13][i][j]==0){ans[13][l[13]][0]=i+1,ans[13][l[13]][1]=j+1;countj=0;
tmp=j;
while(a[13][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[13][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[13][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[13][l[13]++][2]=0;
while(a[13][i][j]!=1&&j<m)a[13][i][j++]=1;}
else{ans[13][l[13]++][2]=1,tmp1=i;
while(a[13][tmp1][j]!=1&&tmp1<n)a[13][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[13][tmp1][j]!=1&&tmp1>=0)a[13][tmp1--][j]=1;}}
for(i=0;i<m;i++)for(j=0;j<n;j++)if(a[14][j][i]==0){ans[14][l[14]][0]=j+1,ans[14][l[14]][1]=i+1,countj=0,tmp=j;
while(a[14][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[14][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[14][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[14][l[14]++][2]=1;
while(a[14][j][i]!=1&&j<n)a[14][j++][i]=1;}
else{ans[14][l[14]++][2]=0;
tmp1=i;
while(a[14][j][tmp1]!=1&&tmp1<m)a[14][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[14][j][tmp1]!=1&&tmp1>=0)a[14][j][tmp1--]=1;}}
for(i=m-1;i>=0;i--)for(j=n-1;j>=0;j--)if(a[15][j][i]==0){ans[15][l[15]][0]=j+1,ans[15][l[15]][1]=i+1,countj=0,tmp=j;
while(a[15][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[15][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[15][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[15][l[15]++][2]=1;
while(a[15][j][i]!=1&&j>=0)a[15][j--][i]=1;}
else{ans[15][l[15]++][2]=0;
tmp1=i;
while(a[15][j][tmp1]!=1&&tmp1<m)a[15][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[15][j][tmp1]!=1&&tmp1>=0)a[15][j][tmp1--]=1;}}
for(i=0;i<m;i++)for(j=n-1;j>=0;j--)if(a[16][j][i]==0){ans[16][l[16]][0]=j+1,ans[16][l[16]][1]=i+1,countj=0,tmp=j;
while(a[16][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[16][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[16][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[16][l[16]++][2]=1;
while(a[16][j][i]!=1&&j>=0)a[16][j--][i]=1;}
else{ans[16][l[16]++][2]=0;
tmp1=i;
while(a[16][j][tmp1]!=1&&tmp1<m)a[16][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[16][j][tmp1]!=1&&tmp1>=0)a[16][j][tmp1]=1;}}
for(i=m-1;i>=0;i--)for(j=0;j<n;j++)if(a[17][j][i]==0){ans[17][l[17]][0]=j+1,ans[17][l[17]][1]=i+1,countj=0,tmp=j;
while(a[17][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[17][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[17][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[17][l[17]++][2]=1;
while(a[17][j][i]!=1&&j<n)a[17][j++][i]=1;}
else{ans[17][l[17]++][2]=0;
tmp1=i;
while(a[17][j][tmp1]!=1&&tmp1<m)a[17][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[17][j][tmp1]!=1&&tmp1>=0)a[17][j][tmp1--]=1;}}
for(i=0;i<n;i++)if(i%6<3){for(j=0;j<m;j++)if(a[18][i][j]==0){ans[18][l[18]][0]=i+1,ans[18][l[18]][1]=j+1,countj=0,tmp=j;
while(a[18][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[18][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[18][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[18][l[18]++][2]=0;
while(a[18][i][j]!=1&&j<m)a[18][i][j++]=1;}
else{ans[18][l[18]++][2]=1,tmp1=i;
while(a[18][tmp1][j]!=1&&tmp1<n)a[18][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[18][tmp1][j]!=1&&tmp1>=0)a[18][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[18][i][j]==0){ans[18][l[18]][0]=i+1,ans[18][l[18]][1]=j+1,countj=0,tmp=j;
while(a[18][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[18][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[18][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[18][l[18]++][2]=0;
while(a[18][i][j]!=1&&j>=0)a[18][i][j--]=1;}
else{ans[18][l[18]++][2]=1,tmp1=i;
while(a[18][tmp1][j]!=1&&tmp1<n)a[18][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[18][tmp1][j]!=1&&tmp1>=0)a[18][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%6<3){for(j=m-1;j>=0;j--)if(a[19][i][j]==0){ans[19][l[19]][0]=i+1,ans[19][l[19]][1]=j+1,countj=0,tmp=j;
while(a[19][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[19][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[19][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[19][l[19]++][2]=0;
while(a[19][i][j]!=1&&j>=0)a[19][i][j--]=1;}
else{ans[19][l[19]++][2]=1,tmp1=i;
while(a[19][tmp1][j]!=1&&tmp1<n)a[19][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[19][tmp1][j]!=1&&tmp1>=0)a[19][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[19][i][j]==0){ans[19][l[19]][0]=i+1,ans[19][l[19]][1]=j+1,countj=0,tmp=j;
while(a[19][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[19][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[19][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[19][l[19]++][2]=0;
while(a[19][i][j]!=1&&j<m)a[19][i][j++]=1;}
else{ans[19][l[19]++][2]=1,tmp1=i;
while(a[19][tmp1][j]!=1&&tmp1<n)a[19][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[19][tmp1][j]!=1&&tmp1>=0)a[19][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%6<3){for(j=0;j<n;j++)if(a[20][j][i]==0){ans[20][l[20]][0]=j+1,ans[20][l[20]][1]=i+1,countj=0,tmp=j;
while(a[20][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[20][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[20][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[20][l[20]++][2]=1;
while(a[20][j][i]!=1&&j<n)a[20][j++][i]=1;}
else{ans[20][l[20]++][2]=0;
tmp1=i;
while(a[20][j][tmp1]!=1&&tmp1<m)a[20][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[20][j][tmp1]!=1&&tmp1>=0)a[20][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[20][j][i]==0){ans[20][l[20]][0]=j+1,ans[20][l[20]][1]=i+1,countj=0,tmp=j;
while(a[20][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[20][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[20][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[20][l[20]++][2]=1;
while(a[20][j][i]!=1&&j>=0)a[20][j--][i]=1;}
else{ans[20][l[20]++][2]=0;
tmp1=i;
while(a[20][j][tmp1]!=1&&tmp1<m)a[20][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[20][j][tmp1]!=1&&tmp1>=0)a[20][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%6<3){for(j=n-1;j>=0;j--)if(a[21][j][i]==0){ans[21][l[21]][0]=j+1,ans[21][l[21]][1]=i+1,countj=0,tmp=j;
while(a[21][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[21][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[21][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[21][l[21]++][2]=1;
while(a[21][j][i]!=1&&j>=0)a[21][j--][i]=1;}
else{ans[21][l[21]++][2]=0;
tmp1=i;
while(a[21][j][tmp1]!=1&&tmp1<m)a[21][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[21][j][tmp1]!=1&&tmp1>=0)a[21][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[21][j][i]==0){ans[21][l[21]][0]=j+1,ans[21][l[21]][1]=i+1,countj=0,tmp=j;
while(a[21][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[21][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[21][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[21][l[21]++][2]=1;
while(a[21][j][i]!=1&&j<n)a[21][j++][i]=1;}
else{ans[21][l[21]++][2]=0;
tmp1=i;
while(a[21][j][tmp1]!=1&&tmp1<m)a[21][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[21][j][tmp1]!=1&&tmp1>=0)a[21][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%6<3){for(j=0;j<m;j++)if(a[22][i][j]==0){ans[22][l[22]][0]=i+1,ans[22][l[22]][1]=j+1,countj=0,tmp=j;
while(a[22][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[22][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[22][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[22][l[22]++][2]=0;
while(a[22][i][j]!=1&&j<m)a[22][i][j++]=1;}
else{ans[22][l[22]++][2]=1,tmp1=i;
while(a[22][tmp1][j]!=1&&tmp1<n)a[22][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[22][tmp1][j]!=1&&tmp1>=0)a[22][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[22][i][j]==0){ans[22][l[22]][0]=i+1,ans[22][l[22]][1]=j+1,countj=0,tmp=j;
while(a[22][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[22][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[22][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[22][l[22]++][2]=0;
while(a[22][i][j]!=1&&j>=0)a[22][i][j--]=1;}
else{ans[22][l[22]++][2]=1,tmp1=i;
while(a[22][tmp1][j]!=1&&tmp1<n)a[22][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[22][tmp1][j]!=1&&tmp1>=0)a[22][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%6<3){for(j=m-1;j>=0;j--)if(a[23][i][j]==0){ans[23][l[23]][0]=i+1,ans[23][l[23]][1]=j+1,countj=0,tmp=j;
while(a[23][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[23][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[23][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[23][l[23]++][2]=0;
while(a[23][i][j]!=1&&j>=0)a[23][i][j--]=1;}
else{ans[23][l[23]++][2]=1,tmp1=i;
while(a[23][tmp1][j]!=1&&tmp1<n)a[23][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[23][tmp1][j]!=1&&tmp1>=0)a[23][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[23][i][j]==0){ans[23][l[23]][0]=i+1,ans[23][l[23]][1]=j+1,countj=0,tmp=j;
while(a[23][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[23][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[23][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[23][l[23]++][2]=0;
while(a[23][i][j]!=1&&j<m)a[23][i][j++]=1;}
else{ans[23][l[23]++][2]=1,tmp1=i;
while(a[23][tmp1][j]!=1&&tmp1<n)a[23][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[23][tmp1][j]!=1&&tmp1>=0)a[23][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%6<3){for(j=0;j<n;j++)if(a[24][j][i]==0){ans[24][l[24]][0]=j+1,ans[24][l[24]][1]=i+1,countj=0,tmp=j;
while(a[24][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[24][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[24][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[24][l[24]++][2]=1;
while(a[24][j][i]!=1&&j<n)a[24][j++][i]=1;}
else{ans[24][l[24]++][2]=0;
tmp1=i;
while(a[24][j][tmp1]!=1&&tmp1<m)a[24][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[24][j][tmp1]!=1&&tmp1>=0)a[24][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[24][j][i]==0){ans[24][l[24]][0]=j+1,ans[24][l[24]][1]=i+1,countj=0,tmp=j;
while(a[24][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[24][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[24][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[24][l[24]++][2]=1;
while(a[24][j][i]!=1&&j>=0)a[24][j--][i]=1;}
else{ans[24][l[24]++][2]=0;
tmp1=i;
while(a[24][j][tmp1]!=1&&tmp1<m)a[24][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[24][j][tmp1]!=1&&tmp1>=0)a[24][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%6<3){for(j=n-1;j>=0;j--)if(a[25][j][i]==0){ans[25][l[25]][0]=j+1,ans[25][l[25]][1]=i+1,countj=0,tmp=j;
while(a[25][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[25][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[25][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[25][l[25]++][2]=1;
while(a[25][j][i]!=1&&j>=0)a[25][j--][i]=1;}
else{ans[25][l[25]++][2]=0;
tmp1=i;
while(a[25][j][tmp1]!=1&&tmp1<m)a[25][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[25][j][tmp1]!=1&&tmp1>=0)a[25][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[25][j][i]==0){ans[25][l[25]][0]=j+1,ans[25][l[25]][1]=i+1,countj=0,tmp=j;
while(a[25][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[25][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[25][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[25][l[25]++][2]=1;
while(a[25][j][i]!=1&&j<n)a[25][j++][i]=1;}
else{ans[25][l[25]++][2]=0;
tmp1=i;
while(a[25][j][tmp1]!=1&&tmp1<m)a[25][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[25][j][tmp1]!=1&&tmp1>=0)a[25][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%6>2){for(j=0;j<m;j++)if(a[26][i][j]==0){ans[26][l[26]][0]=i+1,ans[26][l[26]][1]=j+1,countj=0,tmp=j;
while(a[26][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[26][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[26][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[26][l[26]++][2]=0;
while(a[26][i][j]!=1&&j<m)a[26][i][j++]=1;}
else{ans[26][l[26]++][2]=1,tmp1=i;
while(a[26][tmp1][j]!=1&&tmp1<n)a[26][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[26][tmp1][j]!=1&&tmp1>=0)a[26][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[26][i][j]==0){ans[26][l[26]][0]=i+1,ans[26][l[26]][1]=j+1,countj=0,tmp=j;
while(a[26][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[26][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[26][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[26][l[26]++][2]=0;
while(a[26][i][j]!=1&&j>=0)a[26][i][j--]=1;}
else{ans[26][l[26]++][2]=1,tmp1=i;
while(a[26][tmp1][j]!=1&&tmp1<n)a[26][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[26][tmp1][j]!=1&&tmp1>=0)a[26][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%6>2){for(j=m-1;j>=0;j--)if(a[27][i][j]==0){ans[27][l[27]][0]=i+1,ans[27][l[27]][1]=j+1,countj=0,tmp=j;
while(a[27][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[27][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[27][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[27][l[27]++][2]=0;
while(a[27][i][j]!=1&&j>=0)a[27][i][j--]=1;}
else{ans[27][l[27]++][2]=1,tmp1=i;
while(a[27][tmp1][j]!=1&&tmp1<n)a[27][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[27][tmp1][j]!=1&&tmp1>=0)a[27][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[27][i][j]==0){ans[27][l[27]][0]=i+1,ans[27][l[27]][1]=j+1,countj=0,tmp=j;
while(a[27][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[27][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[27][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[27][l[27]++][2]=0;
while(a[27][i][j]!=1&&j<m)a[27][i][j++]=1;}
else{ans[27][l[27]++][2]=1,tmp1=i;
while(a[27][tmp1][j]!=1&&tmp1<n)a[27][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[27][tmp1][j]!=1&&tmp1>=0)a[27][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%6>2){for(j=0;j<n;j++)if(a[28][j][i]==0){ans[28][l[28]][0]=j+1,ans[28][l[28]][1]=i+1,countj=0,tmp=j;
while(a[28][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[28][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[28][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[28][l[28]++][2]=1;
while(a[28][j][i]!=1&&j<n)a[28][j++][i]=1;}
else{ans[28][l[28]++][2]=0;
tmp1=i;
while(a[28][j][tmp1]!=1&&tmp1<m)a[28][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[28][j][tmp1]!=1&&tmp1>=0)a[28][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[28][j][i]==0){ans[28][l[28]][0]=j+1,ans[28][l[28]][1]=i+1,countj=0,tmp=j;
while(a[28][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[28][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[28][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[28][l[28]++][2]=1;
while(a[28][j][i]!=1&&j>=0)a[28][j--][i]=1;}
else{ans[28][l[28]++][2]=0;
tmp1=i;
while(a[28][j][tmp1]!=1&&tmp1<m)a[28][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[28][j][tmp1]!=1&&tmp1>=0)a[28][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%6>2){for(j=n-1;j>=0;j--)if(a[29][j][i]==0){ans[29][l[29]][0]=j+1,ans[29][l[29]][1]=i+1,countj=0,tmp=j;
while(a[29][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[29][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[29][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[29][l[29]++][2]=1;
while(a[29][j][i]!=1&&j>=0)a[29][j--][i]=1;}
else{ans[29][l[29]++][2]=0;
tmp1=i;
while(a[29][j][tmp1]!=1&&tmp1<m)a[29][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[29][j][tmp1]!=1&&tmp1>=0)a[29][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[29][j][i]==0){ans[29][l[29]][0]=j+1,ans[29][l[29]][1]=i+1,countj=0,tmp=j;
while(a[29][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[29][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[29][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[29][l[29]++][2]=1;
while(a[29][j][i]!=1&&j<n)a[29][j++][i]=1;}
else{ans[29][l[29]++][2]=0;
tmp1=i;
while(a[29][j][tmp1]!=1&&tmp1<m)a[29][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[29][j][tmp1]!=1&&tmp1>=0)a[29][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%6>2){for(j=0;j<m;j++)if(a[30][i][j]==0){ans[30][l[30]][0]=i+1,ans[30][l[30]][1]=j+1,countj=0,tmp=j;
while(a[30][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[30][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[30][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[30][l[30]++][2]=0;
while(a[30][i][j]!=1&&j<m)a[30][i][j++]=1;}
else{ans[30][l[30]++][2]=1,tmp1=i;
while(a[30][tmp1][j]!=1&&tmp1<n)a[30][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[30][tmp1][j]!=1&&tmp1>=0)a[30][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[30][i][j]==0){ans[30][l[30]][0]=i+1,ans[30][l[30]][1]=j+1,countj=0,tmp=j;
while(a[30][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[30][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[30][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[30][l[30]++][2]=0;
while(a[30][i][j]!=1&&j>=0)a[30][i][j--]=1;}
else{ans[30][l[30]++][2]=1,tmp1=i;
while(a[30][tmp1][j]!=1&&tmp1<n)a[30][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[30][tmp1][j]!=1&&tmp1>=0)a[30][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%6>2){for(j=m-1;j>=0;j--)if(a[31][i][j]==0){ans[31][l[31]][0]=i+1,ans[31][l[31]][1]=j+1,countj=0,tmp=j;
while(a[31][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[31][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[31][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[31][l[31]++][2]=0;
while(a[31][i][j]!=1&&j>=0)a[31][i][j--]=1;}
else{ans[31][l[31]++][2]=1,tmp1=i;
while(a[31][tmp1][j]!=1&&tmp1<n)a[31][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[31][tmp1][j]!=1&&tmp1>=0)a[31][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[31][i][j]==0){ans[31][l[31]][0]=i+1,ans[31][l[31]][1]=j+1,countj=0,tmp=j;
while(a[31][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[31][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[31][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[31][l[31]++][2]=0;
while(a[31][i][j]!=1&&j<m)a[31][i][j++]=1;}
else{ans[31][l[31]++][2]=1,tmp1=i;
while(a[31][tmp1][j]!=1&&tmp1<n)a[31][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[31][tmp1][j]!=1&&tmp1>=0)a[31][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%6>2){for(j=0;j<n;j++)if(a[32][j][i]==0){ans[32][l[32]][0]=j+1,ans[32][l[32]][1]=i+1,countj=0,tmp=j;
while(a[32][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[32][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[32][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[32][l[32]++][2]=1;
while(a[32][j][i]!=1&&j<n)a[32][j++][i]=1;}
else{ans[32][l[32]++][2]=0;
tmp1=i;
while(a[32][j][tmp1]!=1&&tmp1<m)a[32][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[32][j][tmp1]!=1&&tmp1>=0)a[32][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[32][j][i]==0){ans[32][l[32]][0]=j+1,ans[32][l[32]][1]=i+1,countj=0,tmp=j;
while(a[32][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[32][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[32][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[32][l[32]++][2]=1;
while(a[32][j][i]!=1&&j>=0)a[32][j--][i]=1;}
else{ans[32][l[32]++][2]=0;
tmp1=i;
while(a[32][j][tmp1]!=1&&tmp1<m)a[32][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[32][j][tmp1]!=1&&tmp1>=0)a[32][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%6>2){for(j=n-1;j>=0;j--)if(a[33][j][i]==0){ans[33][l[33]][0]=j+1,ans[33][l[33]][1]=i+1,countj=0,tmp=j;
while(a[33][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[33][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[33][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[33][l[33]++][2]=1;
while(a[33][j][i]!=1&&j>=0)a[33][j--][i]=1;}
else{ans[33][l[33]++][2]=0;
tmp1=i;
while(a[33][j][tmp1]!=1&&tmp1<m)a[33][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[33][j][tmp1]!=1&&tmp1>=0)a[33][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[33][j][i]==0){ans[33][l[33]][0]=j+1,ans[33][l[33]][1]=i+1,countj=0,tmp=j;
while(a[33][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[33][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[33][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[33][l[33]++][2]=1;
while(a[33][j][i]!=1&&j<n)a[33][j++][i]=1;}
else{ans[33][l[33]++][2]=0;
tmp1=i;
while(a[33][j][tmp1]!=1&&tmp1<m)a[33][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[33][j][tmp1]!=1&&tmp1>=0)a[33][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%4<2){for(j=0;j<m;j++)if(a[34][i][j]==0){ans[34][l[34]][0]=i+1,ans[34][l[34]][1]=j+1,countj=0,tmp=j;
while(a[34][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[34][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[34][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[34][l[34]++][2]=0;
while(a[34][i][j]!=1&&j<m)a[34][i][j++]=1;}
else{ans[34][l[34]++][2]=1,tmp1=i;
while(a[34][tmp1][j]!=1&&tmp1<n)a[34][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[34][tmp1][j]!=1&&tmp1>=0)a[34][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[34][i][j]==0){ans[34][l[34]][0]=i+1,ans[34][l[34]][1]=j+1,countj=0,tmp=j;
while(a[34][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[34][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[34][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[34][l[34]++][2]=0;
while(a[34][i][j]!=1&&j>=0)a[34][i][j--]=1;}
else{ans[34][l[34]++][2]=1,tmp1=i;
while(a[34][tmp1][j]!=1&&tmp1<n)a[34][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[34][tmp1][j]!=1&&tmp1>=0)a[34][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%4<2){for(j=m-1;j>=0;j--)if(a[35][i][j]==0){ans[35][l[35]][0]=i+1,ans[35][l[35]][1]=j+1,countj=0,tmp=j;
while(a[35][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[35][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[35][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[35][l[35]++][2]=0;
while(a[35][i][j]!=1&&j>=0)a[35][i][j--]=1;}
else{ans[35][l[35]++][2]=1,tmp1=i;
while(a[35][tmp1][j]!=1&&tmp1<n)a[35][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[35][tmp1][j]!=1&&tmp1>=0)a[35][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[35][i][j]==0){ans[35][l[35]][0]=i+1,ans[35][l[35]][1]=j+1,countj=0,tmp=j;
while(a[35][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[35][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[35][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[35][l[35]++][2]=0;
while(a[35][i][j]!=1&&j<m)a[35][i][j++]=1;}
else{ans[35][l[35]++][2]=1,tmp1=i;
while(a[35][tmp1][j]!=1&&tmp1<n)a[35][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[35][tmp1][j]!=1&&tmp1>=0)a[35][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%4<2){for(j=0;j<n;j++)if(a[36][j][i]==0){ans[36][l[36]][0]=j+1,ans[36][l[36]][1]=i+1,countj=0,tmp=j;
while(a[36][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[36][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[36][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[36][l[36]++][2]=1;
while(a[36][j][i]!=1&&j<n)a[36][j++][i]=1;}
else{ans[36][l[36]++][2]=0;
tmp1=i;
while(a[36][j][tmp1]!=1&&tmp1<m)a[36][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[36][j][tmp1]!=1&&tmp1>=0)a[36][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[36][j][i]==0){ans[36][l[36]][0]=j+1,ans[36][l[36]][1]=i+1,countj=0,tmp=j;
while(a[36][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[36][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[36][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[36][l[36]++][2]=1;
while(a[36][j][i]!=1&&j>=0)a[36][j--][i]=1;}
else{ans[36][l[36]++][2]=0;
tmp1=i;
while(a[36][j][tmp1]!=1&&tmp1<m)a[36][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[36][j][tmp1]!=1&&tmp1>=0)a[36][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%4<2){for(j=n-1;j>=0;j--)if(a[37][j][i]==0){ans[37][l[37]][0]=j+1,ans[37][l[37]][1]=i+1,countj=0,tmp=j;
while(a[37][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[37][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[37][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[37][l[37]++][2]=1;
while(a[37][j][i]!=1&&j>=0)a[37][j--][i]=1;}
else{ans[37][l[37]++][2]=0;
tmp1=i;
while(a[37][j][tmp1]!=1&&tmp1<m)a[37][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[37][j][tmp1]!=1&&tmp1>=0)a[37][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[37][j][i]==0){ans[37][l[37]][0]=j+1,ans[37][l[37]][1]=i+1,countj=0,tmp=j;
while(a[37][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[37][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[37][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[37][l[37]++][2]=1;
while(a[37][j][i]!=1&&j<n)a[37][j++][i]=1;}
else{ans[37][l[37]++][2]=0;
tmp1=i;
while(a[37][j][tmp1]!=1&&tmp1<m)a[37][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[37][j][tmp1]!=1&&tmp1>=0)a[37][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%4<2){for(j=0;j<m;j++)if(a[38][i][j]==0){ans[38][l[38]][0]=i+1,ans[38][l[38]][1]=j+1,countj=0,tmp=j;
while(a[38][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[38][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[38][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[38][l[38]++][2]=0;
while(a[38][i][j]!=1&&j<m)a[38][i][j++]=1;}
else{ans[38][l[38]++][2]=1,tmp1=i;
while(a[38][tmp1][j]!=1&&tmp1<n)a[38][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[38][tmp1][j]!=1&&tmp1>=0)a[38][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[38][i][j]==0){ans[38][l[38]][0]=i+1,ans[38][l[38]][1]=j+1,countj=0,tmp=j;
while(a[38][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[38][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[38][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[38][l[38]++][2]=0;
while(a[38][i][j]!=1&&j>=0)a[38][i][j--]=1;}
else{ans[38][l[38]++][2]=1,tmp1=i;
while(a[38][tmp1][j]!=1&&tmp1<n)a[38][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[38][tmp1][j]!=1&&tmp1>=0)a[38][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%4<2){for(j=m-1;j>=0;j--)if(a[39][i][j]==0){ans[39][l[39]][0]=i+1,ans[39][l[39]][1]=j+1,countj=0,tmp=j;
while(a[39][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[39][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[39][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[39][l[39]++][2]=0;
while(a[39][i][j]!=1&&j>=0)a[39][i][j--]=1;}
else{ans[39][l[39]++][2]=1,tmp1=i;
while(a[39][tmp1][j]!=1&&tmp1<n)a[39][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[39][tmp1][j]!=1&&tmp1>=0)a[39][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[39][i][j]==0){ans[39][l[39]][0]=i+1,ans[39][l[39]][1]=j+1,countj=0,tmp=j;
while(a[39][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[39][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[39][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[39][l[39]++][2]=0;
while(a[39][i][j]!=1&&j<m)a[39][i][j++]=1;}
else{ans[39][l[39]++][2]=1,tmp1=i;
while(a[39][tmp1][j]!=1&&tmp1<n)a[39][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[39][tmp1][j]!=1&&tmp1>=0)a[39][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%4<2){for(j=0;j<n;j++)if(a[40][j][i]==0){ans[40][l[40]][0]=j+1,ans[40][l[40]][1]=i+1,countj=0,tmp=j;
while(a[40][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[40][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[40][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[40][l[40]++][2]=1;
while(a[40][j][i]!=1&&j<n)a[40][j++][i]=1;}
else{ans[40][l[40]++][2]=0;
tmp1=i;
while(a[40][j][tmp1]!=1&&tmp1<m)a[40][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[40][j][tmp1]!=1&&tmp1>=0)a[40][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[40][j][i]==0){ans[40][l[40]][0]=j+1,ans[40][l[40]][1]=i+1,countj=0,tmp=j;
while(a[40][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[40][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[40][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[40][l[40]++][2]=1;
while(a[40][j][i]!=1&&j>=0)a[40][j--][i]=1;}
else{ans[40][l[40]++][2]=0;
tmp1=i;
while(a[40][j][tmp1]!=1&&tmp1<m)a[40][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[40][j][tmp1]!=1&&tmp1>=0)a[40][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%4<2){for(j=n-1;j>=0;j--)if(a[41][j][i]==0){ans[41][l[41]][0]=j+1,ans[41][l[41]][1]=i+1,countj=0,tmp=j;
while(a[41][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[41][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[41][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[41][l[41]++][2]=1;
while(a[41][j][i]!=1&&j>=0)a[41][j--][i]=1;}
else{ans[41][l[41]++][2]=0;
tmp1=i;
while(a[41][j][tmp1]!=1&&tmp1<m)a[41][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[41][j][tmp1]!=1&&tmp1>=0)a[41][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[41][j][i]==0){ans[41][l[41]][0]=j+1,ans[41][l[41]][1]=i+1,countj=0,tmp=j;
while(a[41][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[41][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[41][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[41][l[41]++][2]=1;
while(a[41][j][i]!=1&&j<n)a[41][j++][i]=1;}
else{ans[41][l[41]++][2]=0;
tmp1=i;
while(a[41][j][tmp1]!=1&&tmp1<m)a[41][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[41][j][tmp1]!=1&&tmp1>=0)a[41][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%4>1){for(j=0;j<m;j++)if(a[42][i][j]==0){ans[42][l[42]][0]=i+1,ans[42][l[42]][1]=j+1,countj=0,tmp=j;
while(a[42][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[42][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[42][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[42][l[42]++][2]=0;
while(a[42][i][j]!=1&&j<m)a[42][i][j++]=1;}
else{ans[42][l[42]++][2]=1,tmp1=i;
while(a[42][tmp1][j]!=1&&tmp1<n)a[42][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[42][tmp1][j]!=1&&tmp1>=0)a[42][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[42][i][j]==0){ans[42][l[42]][0]=i+1,ans[42][l[42]][1]=j+1,countj=0,tmp=j;
while(a[42][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[42][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[42][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[42][l[42]++][2]=0;
while(a[42][i][j]!=1&&j>=0)a[42][i][j--]=1;}
else{ans[42][l[42]++][2]=1,tmp1=i;
while(a[42][tmp1][j]!=1&&tmp1<n)a[42][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[42][tmp1][j]!=1&&tmp1>=0)a[42][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%4>1){for(j=m-1;j>=0;j--)if(a[43][i][j]==0){ans[43][l[43]][0]=i+1,ans[43][l[43]][1]=j+1,countj=0,tmp=j;
while(a[43][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[43][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[43][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[43][l[43]++][2]=0;
while(a[43][i][j]!=1&&j>=0)a[43][i][j--]=1;}
else{ans[43][l[43]++][2]=1,tmp1=i;
while(a[43][tmp1][j]!=1&&tmp1<n)a[43][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[43][tmp1][j]!=1&&tmp1>=0)a[43][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[43][i][j]==0){ans[43][l[43]][0]=i+1,ans[43][l[43]][1]=j+1,countj=0,tmp=j;
while(a[43][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[43][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[43][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[43][l[43]++][2]=0;
while(a[43][i][j]!=1&&j<m)a[43][i][j++]=1;}
else{ans[43][l[43]++][2]=1,tmp1=i;
while(a[43][tmp1][j]!=1&&tmp1<n)a[43][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[43][tmp1][j]!=1&&tmp1>=0)a[43][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%4>1){for(j=0;j<n;j++)if(a[44][j][i]==0){ans[44][l[44]][0]=j+1,ans[44][l[44]][1]=i+1,countj=0,tmp=j;
while(a[44][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[44][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[44][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[44][l[44]++][2]=1;
while(a[44][j][i]!=1&&j<n)a[44][j++][i]=1;}
else{ans[44][l[44]++][2]=0;
tmp1=i;
while(a[44][j][tmp1]!=1&&tmp1<m)a[44][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[44][j][tmp1]!=1&&tmp1>=0)a[44][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[44][j][i]==0){ans[44][l[44]][0]=j+1,ans[44][l[44]][1]=i+1,countj=0,tmp=j;
while(a[44][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[44][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[44][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[44][l[44]++][2]=1;
while(a[44][j][i]!=1&&j>=0)a[44][j--][i]=1;}
else{ans[44][l[44]++][2]=0;
tmp1=i;
while(a[44][j][tmp1]!=1&&tmp1<m)a[44][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[44][j][tmp1]!=1&&tmp1>=0)a[44][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%4>1){for(j=n-1;j>=0;j--)if(a[45][j][i]==0){ans[45][l[45]][0]=j+1,ans[45][l[45]][1]=i+1,countj=0,tmp=j;
while(a[45][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[45][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[45][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[45][l[45]++][2]=1;
while(a[45][j][i]!=1&&j>=0)a[45][j--][i]=1;}
else{ans[45][l[45]++][2]=0;
tmp1=i;
while(a[45][j][tmp1]!=1&&tmp1<m)a[45][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[45][j][tmp1]!=1&&tmp1>=0)a[45][j][tmp1--]=1;}}}
else{for(j=0;j<n;j++)if(a[45][j][i]==0){ans[45][l[45]][0]=j+1,ans[45][l[45]][1]=i+1,countj=0,tmp=j;
while(a[45][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[45][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[45][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>counti){ans[45][l[45]++][2]=1;
while(a[45][j][i]!=1&&j<n)a[45][j++][i]=1;}
else{ans[45][l[45]++][2]=0;
tmp1=i;
while(a[45][j][tmp1]!=1&&tmp1<m)a[45][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[45][j][tmp1]!=1&&tmp1>=0)a[45][j][tmp1--]=1;}}}
for(i=0;i<n;i++)if(i%4>1){for(j=0;j<m;j++)if(a[46][i][j]==0){ans[46][l[46]][0]=i+1,ans[46][l[46]][1]=j+1,countj=0,tmp=j;
while(a[46][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[46][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[46][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[46][l[46]++][2]=0;
while(a[46][i][j]!=1&&j<m)a[46][i][j++]=1;}
else{ans[46][l[46]++][2]=1,tmp1=i;
while(a[46][tmp1][j]!=1&&tmp1<n)a[46][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[46][tmp1][j]!=1&&tmp1>=0)a[46][tmp1--][j]=1;}}}
else{for(j=m-1;j>=0;j--)if(a[46][i][j]==0){ans[46][l[46]][0]=i+1,ans[46][l[46]][1]=j+1,countj=0,tmp=j;
while(a[46][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[46][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[46][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[46][l[46]++][2]=0;
while(a[46][i][j]!=1&&j>=0)a[46][i][j--]=1;}
else{ans[46][l[46]++][2]=1,tmp1=i;
while(a[46][tmp1][j]!=1&&tmp1<n)a[46][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[46][tmp1][j]!=1&&tmp1>=0)a[46][tmp1--][j]=1;}}}
for(i=n-1;i>=0;i--)if(i%4>1){for(j=m-1;j>=0;j--)if(a[47][i][j]==0){ans[47][l[47]][0]=i+1,ans[47][l[47]][1]=j+1,countj=0,tmp=j;
while(a[47][i][tmp]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[47][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[47][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[47][l[47]++][2]=0;
while(a[47][i][j]!=1&&j>=0)a[47][i][j--]=1;}
else{ans[47][l[47]++][2]=1,tmp1=i;
while(a[47][tmp1][j]!=1&&tmp1<n)a[47][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[47][tmp1][j]!=1&&tmp1>=0)a[47][tmp1--][j]=1;}}}
else{for(j=0;j<m;j++)if(a[47][i][j]==0){ans[47][l[47]][0]=i+1,ans[47][l[47]][1]=j+1,countj=0,tmp=j;
while(a[47][i][tmp]!=1&&tmp<m)tmp++,countj++;
tmp1=i,counti=0;
while(a[47][tmp1][j]!=1&&tmp1<n)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[47][tmp1][j]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[47][l[47]++][2]=0;
while(a[47][i][j]!=1&&j<m)a[47][i][j++]=1;}
else{ans[47][l[47]++][2]=1,tmp1=i;
while(a[47][tmp1][j]!=1&&tmp1<n)a[47][tmp1++][j]=1;
if(i>=0)tmp1=i-1;
while(a[47][tmp1][j]!=1&&tmp1>=0)a[47][tmp1--][j]=1;}}}
for(i=0;i<m;i++)if(i%4>1){for(j=0;j<n;j++)if(a[48][j][i]==0){ans[48][l[48]][0]=j+1,ans[48][l[48]][1]=i+1,countj=0,tmp=j;
while(a[48][tmp][i]!=1&&tmp<n)tmp++,countj++;
tmp1=i,counti=0;
while(a[48][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[48][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[48][l[48]++][2]=1;
while(a[48][j][i]!=1&&j<n)a[48][j++][i]=1;}
else{ans[48][l[48]++][2]=0;
tmp1=i;
while(a[48][j][tmp1]!=1&&tmp1<m)a[48][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[48][j][tmp1]!=1&&tmp1>=0)a[48][j][tmp1--]=1;}}}
else{for(j=n-1;j>=0;j--)if(a[48][j][i]==0){ans[48][l[48]][0]=j+1,ans[48][l[48]][1]=i+1,countj=0,tmp=j;
while(a[48][tmp][i]!=1&&tmp>=0)tmp--,countj++;
tmp1=i,counti=0;
while(a[48][j][tmp1]!=1&&tmp1<m)tmp1++,counti++;
if(i>=0)tmp1=i-1;
while(a[48][j][tmp1]!=1&&tmp1>=0)tmp1--,counti++;
if(countj>=counti){ans[48][l[48]++][2]=1;
while(a[48][j][i]!=1&&j>=0)a[48][j--][i]=1;}
else{ans[48][l[48]++][2]=0;
tmp1=i;
while(a[48][j][tmp1]!=1&&tmp1<m)a[48][j][tmp1++]=1;
if(i>=0)tmp1=i-1;
while(a[48][j][tmp1]!=1&&tmp1>=0)a[48][j][tmp1--]=1;}}}
for(i=m-1;i>=0;i--)if(i%4>1){for(j=n-1;j>=0;j--)if(a[49][j][i]==0){ans[49][l[49]][0]=j+1,ans[49][l[49]][1]=i+1,countj=0,tmp=j;
while(a[49][tmp][i]!=1&&tmp>=0)tmp--,countj++;