#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; struct Timer { Timer() { reset(); } void reset() { beg = chrono::high_resolution_clock::now(); } ll elapsed() { chrono::high_resolution_clock::time_point en = chrono::high_resolution_clock::now(); return chrono::duration_cast(en - beg).count(); } private: chrono::high_resolution_clock::time_point beg; } timer; struct Xor128 { int rand() { unsigned t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return static_cast(w); } int rand(int ub) { int res = rand() % ub; return res < 0 ? res + ub : res; } int rand(int lb, int ub) { return lb + rand(ub - lb); } ll randll() { unsigned long long res = static_cast(rand()) << 32; return static_cast(res | rand()); } ll randll(ll ub) { ll res = randll() % ub; return res < 0 ? res + ub : res; } ll randll(ll lb, ll ub) { return lb + randll(ub - lb); } private: unsigned x = 123456789, y = 362436069, z = 521288629, w = static_cast(time(nullptr)); } xor128; int main() { int n, k; cin >> n >> k; vector l(k); REP(i, k) cin >> l[i]; vector A(n); REP(i, n) cin >> A[i]; int w0 = 0; REP(i, n) REP(j, n) w0 += A[i][j] == '0'; int score = -w0; vector a(k, 0), b(k, 0), c(k, 0), d(k); REP(i, k) d[i] = l[i] - 1; function&, vector&, vector&, vector&)> calc = [&](vector &ai, vector &bi, vector &ci, vector &di) { vector > grid(n, vector(n)); REP(i, n) REP(j, n) grid[i][j] = A[i][j] == '1'; REP(i, k) { if (xor128.rand(2)) { int y = xor128.rand(n - l[i] + 1), x = xor128.rand(n); ai[i] = y; bi[i] = x; ci[i] = y + l[i] - 1; di[i] = x; FOR(j, y, y + l[i]) grid[j][x] = !grid[j][x]; } else { int y = xor128.rand(n), x = xor128.rand(n - l[i] + 1); ai[i] = y; bi[i] = x; ci[i] = y; di[i] = x + l[i] - 1; FOR(j, x, x + l[i]) grid[y][j] = !grid[y][j]; } } int res = -w0; REP(i, n) REP(j, n) res += !grid[i][j]; return res; }; while (timer.elapsed() < 900) { vector ai(k), bi(k), ci(k), di(k); int now = calc(ai, bi, ci, di); if (now > score) { score = now; a = ai; b = bi; c = ci; d = di; } } REP(i, k) cout << a[i] + 1 << ' ' << b[i] + 1 << ' ' << c[i] + 1 << ' ' << d[i] + 1 << '\n'; return 0; }