use std::io::{Read, stdin}; 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![]; for _ in 0..k { ls.push(get!()); } let mut grid = vec![]; for _ in 0..n { let tmp: Vec<_> = get().as_bytes() .iter() .map(|&ch| (ch - b'0') as i32) .collect(); grid.push(tmp); } // println!("{:?}", grid); let mut ans = vec![]; for &l in ls.iter() { let mut max_sum_i = 0; let mut max_sum_j = 0; let mut max_i = 0; let mut max_j = 0; for i in 0..n { let mut sum = 0; for j in 0..l { sum += grid[i][j]; } if sum > max_sum_i { max_sum_i = sum; max_i = i; } let mut sum = 0; for j in 0..l { sum += grid[j][i]; } if sum > max_sum_j { max_sum_j = sum; max_j = i; } } if max_sum_i > max_sum_j { for j in 0..l { grid[max_i][j] = 1 - grid[max_i][j]; } ans.push((true, max_i, l, 0)); } else { for i in 0..l { grid[i][max_j] = 1 - grid[i][max_j]; } ans.push((false, max_j, l, 0)); } } for _ in 0..50 { for i in 0..ans.len() { let (b, j, l, p0) = ans[i]; let mut table = vec![0; n + 1]; let mut max_sum = 0; let mut max_p = 0; if b { for p in p0..p0+l { grid[j][p] = 1 - grid[j][p]; } for p in 0..n { table[p+1] = table[p] + grid[j][p]; } } else { for p in p0..p0+l { grid[p][j] = 1 - grid[p][j]; } for p in 0..n { table[p+1] = table[p] + grid[p][j]; } } for p in 0..n-l+1 { let sum = table[p+l] - table[p]; if sum > max_sum { max_sum = sum; max_p = p; } } if b { for p in 0..l { grid[j][max_p + p] = 1 - grid[j][max_p + p]; } } else { for p in 0..l { grid[max_p + p][j] = 1 - grid[max_p + p][j]; } } ans[i] = (b, j, l, max_p); } } for &(b, i, l, p) in ans.iter() { if b { println!("{0} {1} {0} {2}", i + 1, p + 1, p + l); } else { println!("{1} {0} {2} {0}", i + 1, p + 1, p + l); } } }