fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int oo = 5e2 + 2;
  5.  
  6. int n, m;
  7. int a[oo][oo], b[oo][oo];
  8.  
  9. void solve() {
  10. cin >> n >> m;
  11. for (int i = 1; i <= n; i++) {
  12. string s;
  13. cin >> s;
  14. for (int j = 0; j < m; j++) {
  15. a[i][j + 1] = (int)(s[j] - '0');
  16. }
  17. }
  18.  
  19. for (int i = 1; i <= n; i++) {
  20. string s;
  21. cin >> s;
  22. for (int j = 0; j < m; j++) {
  23. b[i][j + 1] = (int)(s[j] - '0');
  24. }
  25. }
  26.  
  27. for (int i = 1; i <= n; i++) {
  28. int sum_col_a = 0;
  29. int sum_col_b = 0;
  30. for (int j = 1; j <= m; j++) {
  31. sum_col_a += a[i][j];
  32. sum_col_b += b[i][j];
  33. }
  34.  
  35. if (sum_col_a % 3 != sum_col_b % 3) {
  36. cout << "NO\n";
  37. return;
  38. }
  39. }
  40.  
  41. for (int j = 1; j <= m; j++) {
  42. int sum_row_a = 0;
  43. int sum_row_b = 0;
  44. for (int i = 1; i <= n; i++) {
  45. sum_row_a += a[i][j];
  46. sum_row_b += b[i][j];
  47. }
  48.  
  49. if (sum_row_a % 3 != sum_row_b % 3) {
  50. cout << "NO\n";
  51. return;
  52. }
  53. }
  54.  
  55. cout << "YES\n";
  56. }
  57.  
  58.  
  59. int main() {
  60.  
  61. int t;
  62. cin >> t;
  63. while (t--) {
  64. solve();
  65. }
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0s 544KB
stdin
7
3 3
000
000
000
111
111
111
4 4
0000
0000
0000
0000
2100
1200
0012
0021
4 4
1020
1200
1210
0000
0000
1200
2200
0000
3 3
012
012
012
010
111
011
8 8
00000000
00000000
00000000
00000000
00000000
00000000
00000000
10000000
00000000
01200000
02010000
00102000
00020100
00001020
00000210
10000000
2 7
0000000
0000000
2220111
0111222
2 7
0000000
0100010
2220111
1210202
stdout
YES
YES
YES
NO
YES
NO
YES