#include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; mt19937_64 rnd; struct BB { BB(int n) : n(n) { for (int d = 0; d < 2; d++) { for (int i = 0; i < n; i++) { b[d][i] = 0; } } } int search(int l, int& i0, int& j0, int& d0) { int m = -1, s = -10000; for (int d = 0; d < 2; d++) { for (int i = 0; i < n; i++) { ll a = (1LL << l) - 1; ll t = (1LL << n) - 1; if (i > 0) t &= b[d][i - 1]; if (i < n - 1) t &= b[d][i + 1]; else t = 0; t ^= b[d][i]; for (int j = 0; j <= n - l; j++) { int c = __builtin_popcountll(b[d][i] & a); int e = c * 64; //if (l > 5) e -= (j > 0 && get(i, j - 1, d)) + (j + l < n && get(i, j + l, d)); e += __builtin_popcountll(t & a); if (e > s) { s = e; m = c; i0 = i; j0 = j; d0 = d; } a <<= 1; } } } for (int h = 0; h < l; h++) { xor1(i0, j0 + h, d0); } if (d0) swap(i0, j0); return m; } void xor1(int i, int j, int d = 0) { b[d][i] ^= 1LL << j; b[d ^ 1][j] ^= 1LL << i; } ll get(int i, int j, int d) { return b[d][i] >> j & 1; } int count() { int s = 0; for (int i = 0; i < n; i++) { s += __builtin_popcountll(b[0][i]); } return s; } int n; ll b[2][60]; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector> l(k); for (int h = 0; h < k; h++) { cin >> l[h][1]; //l[h][0] = l[h][1] + rnd() % 70; l[h][2] = h; } BB bb(n), bb0(n); for (int i = 0; i < n; i++) { char a[61]; cin >> a; for (int j = 0; j < n; j++) { if (a[j] == '1') bb.xor1(i, j); } } bb0 = bb; int s2 = 0; vector> r(k), r0(k); for (int e = 0; e < 20; e++) { bb = bb0; int s0 = bb.count(); for (int h = 0; h < k; h++) { l[h][0] = l[h][1] + rnd() % (60 + e); } sort(l.begin(), l.end()); reverse(l.begin(), l.end()); int s = 0; for (int h = 0; h < k; h++) { int i, j, d; int l0 = l[h][1]; int m = bb.search(l0, i, j, d); auto& a = r[l[h][2]]; a = { i, j, i, j }; a[0]++; a[1]++; a[2 + d] += 1; a[2 + (d ^ 1)] += l0; s += m - (l0 - m); //cout << m - (l0 - m) << endl; } int s1 = bb.count(); //cout << s << ' ' << s0 << ' ' << s1 << endl; if (s != s0 - s1) { throw; } if (s > s2) { s2 = s; r0 = r; } } for (int h = 0; h < k; h++) { for (int i = 0; i < 4; i++) { cout << r0[h][i] << " \n"[i == 3]; } } return 0; }