int T, n, m; int A[N][N], B[N][N], sum_col[N], sum_row[N]; char str[N]; bool vis_row[N], vis_col[N];
inlinevoidclear(){ for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) A[i][j] = B[i][j] = 0; } for (int i = 1; i <= n; ++i) sum_row[i] = vis_row[i] = 0; for (int i = 1; i <= m; ++i) sum_col[i] = vis_col[i] = 0; }
voidlock_row(int); voidlock_col(int);
voidlock_row(int x){ if (vis_row[x]) return; vis_row[x] = true; for (int i = 1; i <= m; ++i) if (B[x][i] == 1) lock_col(i); }
voidlock_col(int x){ if (vis_col[x]) return; vis_col[x] = true; for (int i = 1; i <= n; ++i) if (B[i][x] == 1) lock_row(i); }
inlineboolcheck(){ bool same = true; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { sum_row[i] += B[i][j]; sum_col[j] += B[i][j]; same &= A[i][j] == B[i][j]; } } if (same) returntrue; for (int i = 1; i <= n; ++i) if (sum_row[i] > 1) lock_row(i); for (int i = 1; i <= m; ++i) if (sum_col[i] > 1) lock_col(i); bool a0 = false, b1 = false; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (vis_row[i] && vis_col[j]) { if (A[i][j] != B[i][j]) returnfalse; } elseif (!vis_row[i] && !vis_col[j]) { a0 |= A[i][j] == 0; b1 |= B[i][j] == 1; } } } if (!a0 || !b1) returnfalse; returntrue; }
intmain(){ std::ios::sync_with_stdio(false); cin >> T; while (T--) { cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> (str + 1); for (int j = 1; j <= m; ++j) A[i][j] = str[j] - '0'; } for (int i = 1; i <= n; ++i) { cin >> (str + 1); for (int j = 1; j <= m; ++j) B[i][j] = str[j] - '0'; } if (check()) cout << "Yes" << endl; elsecout << "No" << endl; clear(); } return0; }
int n, m, W, ans, tot; int h[N], lb[N], rb[N], tmp[N], val[N], f[N][N], sum[N];
inlinevoidlsh(){ for (int i = 1; i <= m; ++i) tmp[i] = lb[i], tmp[i + m] = rb[i]; sort(tmp + 1, tmp + m + m + 1); tot = unique(tmp + 1, tmp + m + m + 1) - tmp - 1; for (int i = 1; i <= m; ++i) { lb[i] = lower_bound(tmp + 1, tmp + tot + 1, lb[i]) - tmp; rb[i] = lower_bound(tmp + 1, tmp + tot + 1, rb[i]) - tmp; } }
inlinevoidmain(){ read(n), read(m), read(W); for (int i = 1; i <= n; ++i) read(h[i]), h[i] = W - h[i]; for (int i = 1; i <= m; ++i) { int x, y; read(x), read(y); lb[i] = rb[i] = x; while (lb[i] > 1 && y > h[lb[i]]) --lb[i]; while (rb[i] < n && y > h[rb[i]]) ++rb[i]; } lsh(); for (int i = 1; i <= m; ++i) { for (int j = lb[i]; j <= rb[i]; ++j) ++val[j]; } for (int i = 1; i <= tot; ++i) val[i] = val[i] * (val[i] - 1) / 2; for (int len = 1; len <= tot; ++len) { for (int i = 1; i + len - 1 <= tot; ++i) { int j = i + len - 1; for (int k = 1; k <= m; ++k) { if (lb[k] >= i && rb[k] <= j) ++sum[lb[k]], --sum[rb[k] + 1]; } for (int k = i; k <= j; ++k) sum[k] += sum[k - 1]; for (int k = i; k <= j; ++k) f[i][j] = max(f[i][j], (sum[k] * (sum[k] - 1) / 2) + f[i][k - 1] + f[k + 1][j]); for (int k = i; k <= j + 1; ++k) sum[k] = 0; } } printf("%d\n", f[1][tot]); }