H, W = map(int, input().split()) S = [input() for _ in range(H)] T = [input() for _ in range(H)] ok1, ok2 = True, True for i in range(H): for j in range(W): ok1 &= S[i][j] != T[i][j] ok2 &= S[i][j] != T[H - 1 - i][W - 1 - j] if ok1 and ok2: ans = 0 for n in range(1, 100): ans += n * (pow(2, -(n-2)*(n-1)/2) - pow(2, -(n-1)*n/2)) print(ans) elif ok1 and not ok2: print(3.5317401904617327) elif not ok1 and ok2: ans = 0 for n in range(1, 100): ans += n * (pow(2, -n*(n-1)/2) - pow(2, -((n-1) * (n) / 2 + 2 * n - 1))) print(ans) else: print(-1)