#include <bits/stdc++.h>
using namespace std;
typedef struct node {
  node *left, *right;
  node *above, *under;
  int i, j;
  node() {
    i = -1;
    j = -1;
    left = right = above = under = nullptr;
  }
};
vector<vector<int>> a;
vector<vector<bool>> flag;
vector<node> rows;
vector<node> columns;
void Insert(int i, int j) {
  node* newNode = new node;
  newNode->i = i;
  newNode->j = j;
  node* p = &rows[i];
  bool inserted = false;
  while (nullptr != p->right) {
    p = p->right;
    if (p->j > j) {
      p->left->right = newNode;
      newNode->left = p->left;
      p->left = newNode;
      newNode->right = p;
      inserted = true;
      break;
    }
  }
  if (!inserted) {
    p->right = newNode;
    newNode->left = p;
  }
  p = &columns[j];
  inserted = false;
  while (nullptr != p->under) {
    p = p->under;
    if (p->i > i) {
      p->above->under = newNode;
      newNode->above = p->above;
      p->above = newNode;
      newNode->under = p;
      inserted = true;
      break;
    }
  }
  if (!inserted) {
    p->under = newNode;
    newNode->above = p;
  }
}
void Delete(node* p) {
  if (nullptr != p->right) {
    p->right->left = p->left;
  }
  p->left->right = p->right;
  if (nullptr != p->under) {
    p->under->above = p->above;
  }
  p->above->under = p->under;
  delete p;
}
void Delete(int i, int j) {
  node* p = &rows[i];
  node* result = nullptr;
  while (nullptr != p->right) {
    p = p->right;
    if (p->j == j) {
      result = p;
      if (nullptr != p->right) {
        p->right->left = p->left;
      }
      p->left->right = p->right;
      if (nullptr != p->under) {
        p->under->above = p->above;
      }
      p->above->under = p->under;
      break;
    }
  }
  if (nullptr != result) delete result;
}
int main() {
  int n, m;
  cin >> n >> m;
  a.resize(n, vector<int>(m));
  flag.resize(n, vector<bool>(m));
  rows.resize(n);
  columns.resize(m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int tmp;
      cin >> tmp;
      a[i][j] = tmp;
    }
  }
  int num = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int tmp;
      cin >> tmp;
      if (a[i][j] != tmp) {
        flag[i][j] = true;
        num++;
        Insert(i, j);
      } else
        flag[i][j] = false;
    }
  }
  if (num % 2 == 1)
    cout << "NO" << endl;
  else {
    int ok = true;
    for (int i = 0; i < n; ++i) {
      if (nullptr == rows[i].right) continue;
      node* p = rows[i].right;
      while (nullptr != p) {
        int rowNum = 0;
        node *n1 = nullptr, *n2 = nullptr, *n3 = nullptr;
        int i4 = -1, j4 = -1;
        while (nullptr != p) {
          if (0 == rowNum) {
            n1 = p;
            if (nullptr == n3) {
              if (nullptr != p->under) {
                n3 = p->under;
              }
            }
            rowNum++;
          } else if (1 == rowNum) {
            if (nullptr != n3) {
              n2 = p;
              i4 = n3->i;
              j4 = p->j;
              break;
            } else {
              if (nullptr != p->under) {
                n2 = p;
                n3 = p->under;
                i4 = n3->i;
                j4 = n1->j;
                break;
              }
            }
          }
          p = p->right;
        }
        if (!(nullptr != n1 && nullptr != n2 && nullptr != n3)) {
          cout << "NO" << endl;
          return 0;
        }
        p = p->right;
        flag[n1->i][n1->j] = false;
        flag[n2->i][n2->j] = false;
        flag[n3->i][n3->j] = false;
        Delete(n1);
        Delete(n2);
        Delete(n3);
        num -= 3;
        if (flag[i4][j4]) {
          flag[i4][j4] = false;
          Delete(i4, j4);
          num--;
        } else {
          flag[i4][j4] = true;
          Insert(i4, j4);
          num++;
        }
        if (0 == num) {
          cout << "YES" << endl;
          return 0;
        }
      }
    }
    cout << "YES" << endl;
  }
  return 0;
}
