#include #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; }; inline int one(int condition){ if(condition){ return 1; }else{ return 1; } } void range_output(range ans_range){ cout << ans_range.y1 + 1 << " " << ans_range.x1 + 1 << " " << ans_range.y2 << " " << ans_range.x2 << endl; } int main(){ int N, K; cin >> N >> K; int L_array[K]; map L; for(int i = 0; i < K; i++){ cin >> L_array[i]; L[L_array[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'); } } map> ranges; for(int i = 0; i < N; i++){ int continuity = 0; for(int j = 0; j < N; j++){ if(space[i][j]){ continuity++; }else if(continuity){ ranges[continuity].push_back({i, j - continuity, i + 1, j}); continuity = 0; } } } map> ans; for(int i = 1; i <= 25; i++){ if(L[i] > ranges[i].size()){ for(int j = 0; j < ranges[i].size(); j++){ ans[i].push_back(ranges[i][j]); } for(int j = int(ranges[i].size()); j < L[i]; j++){ ans[i].push_back({0, 0, 1, i}); } }else{ for(int j = 0; j < L[i]; j++){ ans[i].push_back(ranges[i][j]); } } } for(int i = 0; i < K; i++){ range_output(ans[L_array[i]].back()); ans[L_array[i]].pop_back(); } }