1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def silnia(n):
    wynik = 1;
    for i in range(2, n+1):
        wynik *= i;
    return wynik

def binom(n, k):
    return silnia(n) // (silnia(k) * silnia(n-k))

def ile_krawdzi(n, m, p):
    return ((n-1) * m + n * (m-1)) * (binom(n*m-2, p-1))

def licz_ruchy(plansza):
    n = len(plansza)
    m = len(plansza[0])
    count = 0
    for i in range(n):
        for j in range(m):
            if plansza[i][j] == "O":
                if i - 1 >= 0 and plansza[i-1][j] == ".":
                    count += 1
                if i + 1 < n and plansza[i+1][j] == ".":
                    count += 1
                if j - 1 >= 0 and plansza[i][j-1] == ".":
                    count += 1
                if j + 1 < m and plansza[i][j+1] == ".":
                    count += 1
    return count

def parity(plansza):
    n = len(plansza)
    m = len(plansza[0])

    count = 0
    for i in range(n):
        for j in range(m):
            if (i + j) % 2 == 1 and plansza[i][j] == "O":
                count += 1
    return count % 2


n, m = input().split()
n = int(n)
m = int(m)

start = []
for _ in range(n):
    start.append(input())

input()

goal = []
for _ in range(n):
    goal.append(input())

p = 0
for i in range(n):
    for j in range(m):
        if start[i][j] == "O":
            p += 1

if parity(start) != parity(goal):
    print(0.0)
else:
    print("{:.15f}".format(licz_ruchy(goal) / ile_krawdzi(n, m, p)))