use std::io::{Read, stdin}; use std::cmp::max; fn main() { let mut buf = String::new(); stdin().read_to_string(&mut buf).unwrap(); let mut tok = buf.split_whitespace(); let mut get = || tok.next().unwrap(); macro_rules! get { ($t:ty) => (get().parse::<$t>().unwrap()); () => (get!(usize)); } let n = get!(); let k = get!(); let mut ls = vec![0; k]; let mut lmax = 0; for i in 0..ls.len() { ls[i] = get!(); lmax = max(lmax, ls[i]); } let mut lcount = vec![0; lmax+1]; for &l in ls.iter() { lcount[l] += 1; } let mut field = vec![vec![]; n]; for i in 0..field.len() { let a = get().as_bytes(); for j in 0..a.len() { field[i].push((a[j] - b'0') as i32); } } let mut idx = vec![]; for y in 0..n { for x in 0..n { idx.push((y, x)); } } macro_rules! shuffle { () => { for i in 0..idx.len() { let (y, x) = idx[i]; if field[y][x] != 0 { let j = (i + ls[i % ls.len()]) % idx.len(); idx.swap(i, j); } } for i in 0..idx.len() { let (y, x) = idx[i]; if field[y][x] == 0 { let j = (i + ls[i % ls.len()]) % idx.len(); idx.swap(i, j); } } }} shuffle!(); let mut ans = vec![vec![]; lmax+1]; loop { let mut changes = 0; for &(y, x) in idx.iter() { if field[y][x] == 0 { continue; } let mut gs = vec![]; let mut cnt_h = 1; let mut cnt_v = 1; for p in 10..lmax { if lcount[p+1] == 0 { continue; } let l = 1+p as i32; if x+p < n { cnt_h += field[y][x+p]; let value = -cnt_h * 100000 / l; if cnt_h+cnt_h > l { gs.push((value, p, true)); } } if y+p < n { cnt_v += field[y+p][x]; let value = -cnt_v * 100000 / l; if cnt_v+cnt_v > l { gs.push((value, p, false)); } } } if !gs.is_empty() { changes += 1; gs.sort(); if let Some((_, p, h)) = gs.pop() { lcount[p+1] -= 1; if h { ans[p+1].push((y, x, y, x+p)); for x in x..x+p+1 { field[y][x] = 1 - field[y][x]; } } else { ans[p+1].push((y, x, y+p, x)); for y in y..y+p+1 { field[y][x] = 1 - field[y][x]; } } } } } if changes == 0 { break; } } shuffle!(); loop { let mut changes = 0; for &(y, x) in idx.iter() { if field[y][x] == 0 { continue; } let mut gs = vec![]; let mut cnt_h = 1; let mut cnt_v = 1; for p in 4..lmax { if lcount[p+1] == 0 { continue; } let l = 1+p as i32; if x+p < n { cnt_h += field[y][x+p]; let value = -cnt_h * 100000 / l; if cnt_h+cnt_h > l { gs.push((value, p, true)); } } if y+p < n { cnt_v += field[y+p][x]; let value = -cnt_v * 100000 / l; if cnt_v+cnt_v > l { gs.push((value, p, false)); } } } if !gs.is_empty() { changes += 1; gs.sort(); if let Some((_, p, h)) = gs.pop() { lcount[p+1] -= 1; if h { ans[p+1].push((y, x, y, x+p)); for x in x..x+p+1 { field[y][x] = 1 - field[y][x]; } } else { ans[p+1].push((y, x, y+p, x)); for y in y..y+p+1 { field[y][x] = 1 - field[y][x]; } } } } } if changes == 0 { break; } } shuffle!(); loop { let mut changes = 0; for &(y, x) in idx.iter() { if field[y][x] == 0 { continue; } let mut gs = vec![]; let mut cnt_h = 1; let mut cnt_v = 1; for p in 1..lmax { if lcount[p+1] == 0 { continue; } let l = 1+p as i32; if x+p < n { cnt_h += field[y][x+p]; let value = -cnt_h * 100000 / l; if cnt_h+cnt_h >= l { gs.push((value, p, true)); } } if y+p < n { cnt_v += field[y+p][x]; let value = -cnt_v * 100000 / l; if cnt_v+cnt_v >= l { gs.push((value, p, false)); } } } if !gs.is_empty() { changes += 1; gs.sort(); if let Some((_, p, h)) = gs.pop() { lcount[p+1] -= 1; if h { ans[p+1].push((y, x, y, x+p)); for x in x..x+p+1 { field[y][x] = 1 - field[y][x]; } } else { ans[p+1].push((y, x, y+p, x)); for y in y..y+p+1 { field[y][x] = 1 - field[y][x]; } } } } } if changes == 0 { break; } } shuffle!(); loop { let mut changes = 0; for &(y, x) in idx.iter() { if field[y][x] == 0 { continue; } let mut gs = vec![]; let mut cnt_h = 1; let mut cnt_v = 1; for p in 1..lmax { if lcount[p+1] == 0 { continue; } let l = 1+p as i32; if x+p < n { cnt_h += field[y][x+p]; let value = -cnt_h * 100000 / l; gs.push((value, p, true)); } if y+p < n { cnt_v += field[y+p][x]; let value = -cnt_v * 100000 / l; gs.push((value, p, false)); } } if !gs.is_empty() { changes += 1; gs.sort(); if let Some((_, p, h)) = gs.pop() { lcount[p+1] -= 1; if h { ans[p+1].push((y, x, y, x+p)); for x in x..x+p+1 { field[y][x] = 1 - field[y][x]; } } else { ans[p+1].push((y, x, y+p, x)); for y in y..y+p+1 { field[y][x] = 1 - field[y][x]; } } } } } if changes == 0 { break; } } shuffle!(); loop { let mut changes = 0; for &(y, x) in idx.iter() { if field[y][x] == 0 { continue; } let mut gs = vec![]; let mut cnt_h = 1; let mut cnt_v = 1; for p in 0..lmax { if lcount[p+1] == 0 { continue; } let l = 1+p as i32; if x+p < n { cnt_h += field[y][x+p]; let value = -cnt_h * 100000 / l; gs.push((value, p, true)); } if y+p < n { cnt_v += field[y+p][x]; let value = -cnt_v * 100000 / l; gs.push((value, p, false)); } } if !gs.is_empty() { changes += 1; gs.sort(); if let Some((_, p, h)) = gs.pop() { lcount[p+1] -= 1; if h { ans[p+1].push((y, x, y, x+p)); for x in x..x+p+1 { field[y][x] = 1 - field[y][x]; } } else { ans[p+1].push((y, x, y+p, x)); for y in y..y+p+1 { field[y][x] = 1 - field[y][x]; } } } } } if changes == 0 { break; } } let mut emp = 0; for &l in ls.iter() { if let Some((y1, x1, y2, x2)) = ans[l].pop() { println!("{} {} {} {}", y1+1, x1+1, y2+1, x2+1); } else { println!("{} {} {} {}", n, n-l+1, n, n, ); emp += 1; } } eprintln!("emp {}", emp); }