# from pprint import pprint
from sys import exit

def line_ints(): return list(map(int, input().split()))


n, m = line_ints()

mat = [[False] * (2 * m + 1) for _ in range(2 * n + 1)]

for i in range(2 * n  + 1):
    row = line_ints()
    for j in range(len(row)):
        if row[j] == 1:
            mat[i][2 * j + (i % 2 == 0)] = not mat[i][2 * j + (i % 2 == 0)]

for i in range(2 * n  + 1):
    row = line_ints()
    for j in range(len(row)):
        if row[j] == 1:
            mat[i][2 * j + (i % 2 == 0)] = not mat[i][2 * j + (i % 2 == 0)]
        
#pprint(mat)

for i in range(0, len(mat), 2):
    for j in range(0, len(mat[0]), 2):
        last_move = False
        
        ci, cj = i, j
        while True:
            # print(ci, cj)
            # pprint(mat)
            # UP
            if ci != 0:
                if mat[ci-1][cj]:
                    mat[ci-1][cj] = False
                    ci -= 2
                    last_move = False
                    continue

            # DOWN
            if ci != len(mat) - 1:
                if mat[ci+1][cj]:
                    mat[ci+1][cj] = False
                    ci += 2
                    last_move = False
                    continue

            # LEFT
            if cj != 0:
                if mat[ci][cj-1]:
                    mat[ci][cj-1] = False
                    cj -= 2
                    last_move = False
                    continue
            # RIGHT
            if cj != len(mat[0]) - 1:
                if mat[ci][cj+1]:
                    mat[ci][cj+1] = False
                    cj += 2
                    last_move = False
                    continue


            # UP
            if ci == 0:
                if not last_move:
                    ci = len(mat) - 1
                    last_move = True
                    continue

            # DOWN
            if ci == len(mat) - 1:
                if not last_move:
                    ci = 0
                    last_move = True
                    continue

            # LEFT
            if cj == 0:
                if not last_move:
                    cj = len(mat[0]) - 1
                    last_move = True
                    continue
    
            # RIGHT
            if cj == len(mat[0]) - 1:
                if not last_move:
                    cj = 0
                    last_move = True
                    continue

            if ci == i and cj == j:
                break
            if ci == i and cj == 0 and j == len(mat[0]) -1:
                break
            if ci == i and j == 0 and cj == len(mat[0]) -1:
                break
            if cj == j and ci == 0 and i == len(mat) -1:
                break
            if cj == j and i == 0 and ci == len(mat) -1:
                break

            print("NO")
            exit(0)


if any([any(m) for m in mat]):
    print("NO")
else:
    print("YES")