H,W=map(int,input().split()) S=[input().strip() for _ in range(H)] T=[input().strip() for _ in range(H)] S = [s.replace("#","0").replace(".","#").replace("0",".") for s in S] ans = [3.5, 3.128936827211877] if S==T: print(ans[0]) exit() S = [s[::-1] for s in S[::-1]] if S==T: print(ans[1]) else: print(-1)