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] S2 = [s[::-1] for s in S[::-1]] if S==T and S2==T: print(2.641632560655154) elif S==T: print(3.531740190461733) elif S2==T: print(3.128936827211877) else: print(-1)