import sys

def solve(h, w, s, t):
	b = (1 << w) - 1
	conn = all( s[i] ^ t[i] ^ b == 0 for i in range(h) )
	conn_rev = all( s[i] ^ t[~i] ^ b == 0 for i in range(h) )
	ev = 0
	p = 0
	q = 0
	n = 15
	for i in range(n):
		if i % 2 == 0:
			if conn:
				p = (1 - q) * (1 - 2 ** -i)
				q += p
				ev += (i + 1) * p
		else:
			if conn_rev:
				p = (1 - q) * (1 - 2 ** -i)
				q += p
				ev += (i + 1) * p
		print(i, p, q, file=sys.stderr)
				
	return ev if ev > 0 else -1

h, w = [int(x) for x in input().split()]
s = [int(input().strip().replace("#", "1").replace(".", "0"), 2) for _ in range(h)]
t = [int(input().strip().replace("#", "1").replace(".", "0"), 2) for _ in range(h)]
print(solve(h, w, s, t))