class Problem{ import std.stdio, std.string, std.conv, std.algorithm; import std.math, std.array, std.random; const int L_MAX=25; const int n=60, k=500; int[] li; int[][] a; this(int[] li, int[][] a){ this.li=li.dup; this.a=a.dup; } auto idx=new int[][](30); void init(){ foreach(int i, l; li){ idx[l]~=i; } } int take(int l){ int ret=-1; for(int i=l; i<=L_MAX; i++){ if(idx[i].length>0){ ret=idx[i].back; idx[i].popBack; break; } } return ret; } int take2(){ int[] cand; for(int i=1; i<=L_MAX; i++){ if(idx[i].length>0){ cand~=idx[i].back; } } if(cand.length>0){ int ret=cand.choice; idx[li[ret]].popBack; return ret; }else{ return -1; } } struct Rect{ int a, b, c, d; } auto ans=new Rect[](k); void solve(){ foreach(i; 0..n){ int len=1; foreach(j; 0..(L_MAX+5))if(a[i][j]==1){ int m=take2(); // if(m<0) goto hell; if(m<0) break; ans[m]=Rect(i+1, j+1, i+1, j+li[m]-1+1); foreach(l; j..(j+li[m])) a[i][l]^=1; len++; } } hell:; } void output(){ int un_used=0; foreach(int i, l; li){ auto r=ans[i]; if(r.a>0){ writeln(r.a, " ", r.b, " ", r.c, " ", r.d); }else{ un_used++; writeln(n-1, " ", 1, " ", n-1, " ", l); } } // writeln(un_used); } void count(){ int[int] cnt; foreach(i; 0..25){ int c=0; foreach(j; 0..25){ if(a[i][j]==1) c++; else{ if(c in cnt) cnt[c]++; else cnt[c]=1; c=0; } } } foreach(key; cnt.keys.sort){ writeln(key, " ", cnt[key]); } } void show(){ } } void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, k; rd(n, k); auto li=readln.split.to!(int[]); auto a=new int[][](n, n); foreach(i; 0..n){ auto line=readln.chomp.to!(char[]); foreach(j; 0..n) a[i][j]=line[j]-'0'; } auto problem=new Problem(li, a); problem.init; problem.solve; problem.output; } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }