#include "bits/stdc++.h"
using namespace std;
const int N = 1005;
int B[N][N], n, m, r, c;
int a[N][N];
void upd(int x, int y, int v) {
for(int i = x ; i <= n ; i += (i&-i)) {
for(int j = y ; j <= m ; j += (j&-j)) {
B[i][j] += v;
}
}
}
int sum(int x, int y) {
int ans = 0;
for(int i = x ; i > 0 ; i -= (i&-i)) {
for(int j = y ; j > 0 ; j -= (j&-j)) {
ans += B[i][j];
}
}
return ans;
}
int update(int x1, int y1, int x2, int y2, int v) {
upd(x2 + 1, y2 + 1, v);
upd(x1, y1, v);
upd(x1, y2 + 1, -v);
upd(x2 + 1, y1, -v);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t; cin>>t;
while(t -- ) {
cin>>n>>m>>r>>c;
for(int i = 0; i<=n; i++) for(int j = 0; j<=m; j++) B[i][j] = 0;
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= m ; j++) {
char ai; cin>>ai;
if(ai == '1') a[i][j] = 1;
else a[i][j] = 0;
upd(i, j, a[i][j] + a[i-1][j-1] - a[i-1][j] - a[i][j-1]);
}
}
int ans = 0;
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= m ; j++) {
int get = sum(i, j);
if(get & 1) {
if(i + r - 1 > n || j + c - 1 > m) {
continue;
}
// cout<<i<<" "<<j<<" "<<i+r-1<<" "<<j+c-1<<" "<<get<<"\n";
ans ++;
update(i, j, i + r - 1, j + c - 1, 1);
}
}
}
bool ok = 1;
for(int i = 1; i<=n; i++) {
for(int j = 1; j<=m; j++) {
int get = sum(i, j);
if(get & 1) {
ok = 0;
break;
}
}
if(!ok) break;
}
if(ok) {
cout<<ans<<"\n";
} else {
cout<<"-1\n";
}
}
return 0;
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKIApjb25zdCBpbnQgTiA9IDEwMDU7CiAKaW50IEJbTl1bTl0sIG4sIG0sIHIsIGM7CmludCBhW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QoaSwgaiwgYVtpXVtqXSArIGFbaS0xXVtqLTFdIC0gYVtpLTFdW2pdIC0gYVtpXVtqLTFdKTsKCQkgICAgfQoJCX0KCQlpbnQgYW5zID0gMDsKCQlmb3IoaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrKSB7CgkJICAgIGZvcihpbnQgaiA9IDEgOyBqIDw9IG0gOyBqKyspIHsKCQkgICAgCWludCBnZXQgPSBzdW0oaSwgaik7CQoJCSAgICAJaWYoZ2V0ICYgMSkgewoJCSAgICAJCWlmKGkgKyByIC0gMSA+IG4gfHwgaiArIGMgLSAxID4gbSkgewoJCSAgICAJCQljb250aW51ZTsKCQkJCQl9Ci8vCQkJCQljb3V0PDxpPDwiICI8PGo8PCIgIjw8aStyLTE8PCIgIjw8aitjLTE8PCIgIjw8Z2V0PDwiXG4iOwoJCQkJCWFucyArKzsKCQkgICAgCQl1cGRhdGUoaSwgaiwgaSArIHIgLSAxLCBqICsgYyAtIDEsIDEpOwoJCQkJfQoJCSAgICB9CgkJfQoJCWJvb2wgb2sgPSAxOwoJCWZvcihpbnQgaSA9IDE7IGk8PW47IGkrKykgewoJCQlmb3IoaW50IGogPSAxOyBqPD1tOyBqKyspIHsKCQkJCWludCBnZXQgPSBzdW0oaSwgaik7CgkJCQlpZihnZXQgJiAxKSB7CgkJCQkJb2sgPSAwOwoJCQkJCWJyZWFrOwoJCQkJfQoJCQl9CgkJCWlmKCFvaykgYnJlYWs7CgkJfQoJCWlmKG9rKSB7CgkJCWNvdXQ8PGFuczw8IlxuIjsKCQl9IGVsc2UgewoJCQljb3V0PDwiLTFcbiI7CgkJfQoJfQogICAgcmV0dXJuIDA7Cn0gICAgIA==
Main.java:1: error: illegal character: '#'
#include "bits/stdc++.h"
^
Main.java:1: error: class, interface, or enum expected
#include "bits/stdc++.h"
^
Main.java:5: error: class, interface, or enum expected
const int N = 1005;
^
Main.java:7: error: class, interface, or enum expected
int B[N][N], n, m, r, c;
^
Main.java:8: error: class, interface, or enum expected
int a[N][N];
^
Main.java:11: error: class, interface, or enum expected
void upd(int x, int y, int v) {
^
Main.java:12: error: class, interface, or enum expected
for(int i = x ; i <= n ; i += (i&-i)) {
^
Main.java:12: error: class, interface, or enum expected
for(int i = x ; i <= n ; i += (i&-i)) {
^
Main.java:13: error: class, interface, or enum expected
for(int j = y ; j <= m ; j += (j&-j)) {
^
Main.java:13: error: class, interface, or enum expected
for(int j = y ; j <= m ; j += (j&-j)) {
^
Main.java:15: error: class, interface, or enum expected
}
^
Main.java:20: error: class, interface, or enum expected
for(int i = x ; i > 0 ; i -= (i&-i)) {
^
Main.java:20: error: class, interface, or enum expected
for(int i = x ; i > 0 ; i -= (i&-i)) {
^
Main.java:20: error: class, interface, or enum expected
for(int i = x ; i > 0 ; i -= (i&-i)) {
^
Main.java:21: error: class, interface, or enum expected
for(int j = y ; j > 0 ; j -= (j&-j)) {
^
Main.java:21: error: class, interface, or enum expected
for(int j = y ; j > 0 ; j -= (j&-j)) {
^
Main.java:23: error: class, interface, or enum expected
}
^
Main.java:26: error: class, interface, or enum expected
}
^
Main.java:29: error: class, interface, or enum expected
upd(x1, y1, v);
^
Main.java:30: error: class, interface, or enum expected
upd(x1, y2 + 1, -v);
^
Main.java:31: error: class, interface, or enum expected
upd(x2 + 1, y1, -v);
^
Main.java:32: error: class, interface, or enum expected
}
^
Main.java:37: error: class, interface, or enum expected
cin.tie(0);cout.tie(0);
^
Main.java:37: error: class, interface, or enum expected
cin.tie(0);cout.tie(0);
^
Main.java:38: error: class, interface, or enum expected
int t; cin>>t;
^
Main.java:38: error: class, interface, or enum expected
int t; cin>>t;
^
Main.java:39: error: class, interface, or enum expected
while(t -- ) {
^
Main.java:41: error: class, interface, or enum expected
for(int i = 0; i<=n; i++) for(int j = 0; j<=m; j++) B[i][j] = 0;
^
Main.java:41: error: class, interface, or enum expected
for(int i = 0; i<=n; i++) for(int j = 0; j<=m; j++) B[i][j] = 0;
^
Main.java:41: error: class, interface, or enum expected
for(int i = 0; i<=n; i++) for(int j = 0; j<=m; j++) B[i][j] = 0;
^
Main.java:41: error: class, interface, or enum expected
for(int i = 0; i<=n; i++) for(int j = 0; j<=m; j++) B[i][j] = 0;
^
Main.java:41: error: class, interface, or enum expected
for(int i = 0; i<=n; i++) for(int j = 0; j<=m; j++) B[i][j] = 0;
^
Main.java:42: error: class, interface, or enum expected
for(int i = 1 ; i <= n ; i++) {
^
Main.java:42: error: class, interface, or enum expected
for(int i = 1 ; i <= n ; i++) {
^
Main.java:42: error: class, interface, or enum expected
for(int i = 1 ; i <= n ; i++) {
^
Main.java:43: error: class, interface, or enum expected
for(int j = 1 ; j <= m ; j++) {
^
Main.java:43: error: class, interface, or enum expected
for(int j = 1 ; j <= m ; j++) {
^
Main.java:44: error: class, interface, or enum expected
char ai; cin>>ai;
^
Main.java:45: error: class, interface, or enum expected
if(ai == '1') a[i][j] = 1;
^
Main.java:46: error: class, interface, or enum expected
else a[i][j] = 0;
^
Main.java:47: error: class, interface, or enum expected
upd(i, j, a[i][j] + a[i-1][j-1] - a[i-1][j] - a[i][j-1]);
^
Main.java:48: error: class, interface, or enum expected
}
^
Main.java:51: error: class, interface, or enum expected
for(int i = 1 ; i <= n ; i++) {
^
Main.java:51: error: class, interface, or enum expected
for(int i = 1 ; i <= n ; i++) {
^
Main.java:51: error: class, interface, or enum expected
for(int i = 1 ; i <= n ; i++) {
^
Main.java:52: error: class, interface, or enum expected
for(int j = 1 ; j <= m ; j++) {
^
Main.java:52: error: class, interface, or enum expected
for(int j = 1 ; j <= m ; j++) {
^
Main.java:54: error: class, interface, or enum expected
if(get & 1) {
^
Main.java:57: error: class, interface, or enum expected
}
^
Main.java:60: error: class, interface, or enum expected
update(i, j, i + r - 1, j + c - 1, 1);
^
Main.java:61: error: class, interface, or enum expected
}
^
Main.java:65: error: class, interface, or enum expected
for(int i = 1; i<=n; i++) {
^
Main.java:65: error: class, interface, or enum expected
for(int i = 1; i<=n; i++) {
^
Main.java:65: error: class, interface, or enum expected
for(int i = 1; i<=n; i++) {
^
Main.java:66: error: class, interface, or enum expected
for(int j = 1; j<=m; j++) {
^
Main.java:66: error: class, interface, or enum expected
for(int j = 1; j<=m; j++) {
^
Main.java:68: error: class, interface, or enum expected
if(get & 1) {
^
Main.java:70: error: class, interface, or enum expected
break;
^
Main.java:71: error: class, interface, or enum expected
}
^
Main.java:74: error: class, interface, or enum expected
}
^
Main.java:77: error: class, interface, or enum expected
} else {
^
Main.java:79: error: class, interface, or enum expected
}
^
Main.java:82: error: class, interface, or enum expected
}
^
63 errors