import java.io.*; import java.util.*; public class Main_yukicoder5002_1 { private static Scanner sc; private static Printer pr; private static void solve() { int n = sc.nextInt(); int k = sc.nextInt(); int[] l = new int[k]; for (int i = 0; i < k; i++) { l[i] = sc.nextInt(); } List a = new ArrayList<>(n); for (int i = 0; i < n; i++) { long tmp = 0; char[] s = sc.next().toCharArray(); for (int j = 0; j < n; j++) { if (s[j] == '1') { tmp |= 0x1L << j; } } a.add(tmp); } Random rnd = new Random(0); PriorityQueue pq = new PriorityQueue<>(); pq.add(new Op(a)); for (int i = 0; i < k; i++) { PriorityQueue pq2 = new PriorityQueue<>(); for (int j = 0; j < 2 && !pq.isEmpty(); j++) { Op e = pq.remove(); for (int jj = 0; jj < 100; jj++) { int x = rnd.nextInt(n); int y = rnd.nextInt(n); if (x + l[i] - 1 < n) { Op tmp = e.copy(); for (int ii = x; ii < x + l[i]; ii++) { tmp.a.set(y, tmp.a.get(y) ^ 0x1L << ii); } tmp.set(tmp.a, false, x, y); // if (tmp.score <= e.score + Math.max(3, l[i] / 2)) { pq2.add(tmp); // } } if (y + l[i] - 1 < n) { Op tmp = e.copy(); for (int ii = y; ii < y + l[i]; ii++) { tmp.a.set(ii, tmp.a.get(ii) ^ 0x1L << x); } tmp.set(tmp.a, true, x, y); // if (tmp.score <= e.score + Math.max(3, l[i] / 2)) { pq2.add(tmp); // } } } } pq = pq2; } Op e = pq.remove(); // for (long ee : e.a) { // pr.println(ee); // } // pr.println(e.score); for (int i = 0; i < k; i++) { boolean d = e.d.get(i); int x = e.x.get(i); int y = e.y.get(i); if (!d) { pr.printf("%d %d %d %d\n", y + 1, x + 1, y + 1, x + l[i]); } else { pr.printf("%d %d %d %d\n", y + 1, x + 1, y + l[i], x + 1); } } } static class Op implements Comparable { List a; int score; List d; List x; List y; private Op() { } Op(List a) { this.a = a; for (long e : a) { score += Long.bitCount(e); } d = new ArrayList<>(); x = new ArrayList<>(); y = new ArrayList<>(); } void set(List a, boolean d, int x, int y) { this.a = a; score = 0; for (long e : a) { score += Long.bitCount(e); } this.d.add(d); this.x.add(x); this.y.add(y); } Op copy() { Op tmp = new Op(); tmp.a = new ArrayList<>(this.a); tmp.score = this.score; tmp.d = new ArrayList<>(this.d); tmp.x = new ArrayList<>(this.x); tmp.y = new ArrayList<>(this.y); return tmp; } @Override public int compareTo(Op o) { return Integer.compare(score, o.score); } } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes())); pr = new Printer(System.out); solve(); // pr.close(); pr.flush(); // sc.close(); } static String INPUT = null; private static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } } }