r, c = map(int, input().split())
a = [["OX".find(i) for i in input()] for j in range(r)]
t = [["OX".find(i) for i in input()] for j in range(r)]
same = True
for x in range(c):
for y in range(r):
if a[y][x] != t[y][x]:
same = False
if same:
print(1)
else:
allx = True
for x in range(c):
for y in range(r):
if not a[y][x]:
allx = False
if allx:
print(0)
else:
vr = [0 for _ in range(r)]
vc = [0 for _ in range(c)]
total = 0
for y in range(r):
s = sum(t[y])
total += s
if s <= 1:
vr[y] = 1
for x in range(c):
s = sum(t[y][x] for y in range(r))
if s <= 1:
vc[x] = 1
poss = True
for x in range(c):
for y in range(r):
if a[y][x] == t[y][x] or (not t[y][x] and vr[y] and vc[x]) or (t[y][x] and (vr[y] or vc[x])):
pass
else:
poss = False
print(1 if total > 0 and poss else 0)