#include #include #include using namespace std; template inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } template inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template inline void POSS(T condition){ if(condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; } template inline void Poss(T condition){ if(condition) cout << "Possible" << endl; else cout << "Impossible" << endl; } template inline void First(T condition){ if(condition) cout << "First" << endl; else cout << "Second" << endl; } int character_count(string text, char character){ int ans = 0; for(int i = 0; i < text.size(); i++){ ans += (text[i] == character); } return ans; } long power(long base, long exponent, long module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }} struct position{ int y, x; }; position move_pattern[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); } template void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++){ ans += to_string(*i) + " "; } ans.pop_back(); cout << ans << endl; } long gcd(long a, long b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} #define mod long(1e9 + 7) #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl(long(n)) struct range{ int y1, x1, y2, x2; }; template< class T > struct CumulativeSum2D { vector< vector< T > > data; CumulativeSum2D(int W, int H) : data(W + 1, vector< int >(H + 1, 0)) {} void add(int x, int y, int z) { ++x, ++y; if(x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build() { for(int i = 1; i < data.size(); i++) { for(int j = 1; j < data[i].size(); j++) { data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(int sx, int sy, int gx, int gy) { return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; inline int one(int condition){ if(condition){ return 1; }else{ return 1; } } int main(){ int N, K; cin >> N >> K; int L[K]; for(int i = 0; i < K; i++){ cin >> L[i]; } int space[N][N]; for(int i = 0; i < N; i++){ string space_string; cin >> space_string; for(int j = 0; j < N; j++){ space[i][j] = (space_string[j] == '1'); } } CumulativeSum2D cumulative_sum_space(N, N); for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ cumulative_sum_space.add(i, j, space[i][j]); } } cumulative_sum_space.build(); range ans[K]; for(int i = 0; i < K; i++){ range this_ans = {0, 0, 0, 0}; int this_ans_ans = -1e9; for(int j = 0; j < N; j++){ for(int k = 0; k <= N - L[i]; k++){ if(this_ans_ans < cumulative_sum_space.query(j, k, j + 1, k + L[i])){ this_ans_ans = cumulative_sum_space.query(j, k, j + 1, k + L[i]); this_ans = {j, k, j + 1, k + L[i]}; } if(this_ans_ans < cumulative_sum_space.query(k, j, k + L[i], j + 1)){ this_ans_ans = cumulative_sum_space.query(k, j, k + L[i], j + 1); this_ans = {k, j, k + L[i], j + 1}; } } } for(int j = this_ans.y1; j < this_ans.y2; j++){ for(int k = this_ans.x1; k < this_ans.x2; k++){ space[j][k] ^= 1; cumulative_sum_space.add(j, k, one(space[j][k])); } } ans[i] = this_ans; } for(int i = 0; i < K; i++){ cout << ans[i].y1 + 1 << " " << ans[i].x1 + 1 << " " << ans[i].y2 << " " << ans[i].x2 << endl; } }