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
import sys
from math import factorial

def dwumian(N, K):
    if N < 0 or K < 0:
        return 0
    if K > N:
        return 0
    return factorial(N) // factorial(K) // factorial(N - K)

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

pocz = []
for i in range(n):
    pocz.append(input())
input()
kon = []
for i in range(n):
    kon.append(input())

ile_pocz = 0
ile_kon = 0
sum_pocz = 0
sum_kon = 0
for i in range(n):
    for j in range(m):
        if pocz[i][j] == 'O':
            sum_pocz += i + j
            ile_pocz += 1
        if kon[i][j] == 'O':
            sum_kon += i + j
            ile_kon += 1

if ile_pocz != ile_kon or sum_pocz % 2 != sum_kon % 2:
    print(0)
else:
    ile_kraw = 0
    kier = [(-1, 0), (1, 0), (0, 1), (0, -1)]
    deg = 0
    xd = dwumian(n * m - 2, ile_pocz - 1)
    for x in range(n):
        for y in range(m):
            somsiady = 0
            for dx, dy in kier:
                x1 = x + dx
                y1 = y + dy
                if 0 <= x1 <= n - 1 and 0 <= y1 <= m - 1:
                    somsiady += 1
                    if kon[x1][y1] == '.' and kon[x][y] == 'O':
                        deg += 1
            ile_kraw += somsiady * xd
    ile_kraw /= 2
    #print(deg, file=sys.stderr)
    #print(ile_kraw, file=sys.stderr)
    if ile_kraw > 5 * 10 ** 15:
        print(0)
    else:
        pstwo = deg / ile_kraw
        print("{:.15f}".format(pstwo))