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
#!/usr/bin/env python

import sys
import decimal

PRECOMP = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[1,1],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[2,2],[2,2],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[3,3],[6,6],[3,3],[0,0],[0,0],[0,0],[0,0],[0,0],[4,4],[12,12],[12,12],[4,4],[0,0],[0,0],[0,0],[0,0],[5,5],[20,20],[30,30],[20,20],[5,5],[0,0],[0,0],[0,0],[6,6],[30,30],[60,60],[60,60],[30,30],[6,6],[0,0],[0,0],[1,1],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[4,4],[8,8],[4,4],[0,0],[0,0],[0,0],[0,0],[0,0],[7,7],[28,28],[42,42],[28,28],[7,7],[0,0],[0,0],[0,0],[10,10],[60,60],[150,150],[200,200],[150,150],[60,60],[10,10],[0,0],[13,13],[104,104],[364,364],[728,728],[910,910],[728,728],[364,364],[104,104],[16,16],[160,160],[720,720],[1920,1920],[3360,3360],[4032,4032],[3360,3360],[1920,1920],[19,19],[228,228],[1254,1254],[4180,4180],[9405,9405],[15048,15048],[17556,17556],[15048,15048],[2,2],[2,2],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[7,7],[28,28],[42,42],[28,28],[7,7],[0,0],[0,0],[0,0],[12,12],[84,84],[252,252],[420,420],[420,420],[252,252],[84,84],[12,12],[17,17],[170,170],[765,765],[2040,2040],[3570,3570],[4284,4284],[3570,3570],[2040,2040],[22,22],[286,286],[1716,1716],[6292,6292],[15730,15730],[28314,28314],[37752,37752],[37752,37752],[27,27],[432,432],[3240,3240],[15120,15120],[49140,49140],[117936,117936],[216216,216216],[308880,308880],[32,32],[608,608],[5472,5472],[31008,31008],[124032,124032],[372096,372096],[868224,868224],[1612416,1612416],[3,3],[6,6],[3,3],[0,0],[0,0],[0,0],[0,0],[0,0],[10,10],[60,60],[150,150],[200,200],[150,150],[60,60],[10,10],[0,0],[17,17],[170,170],[765,765],[2040,2040],[3570,3570],[4284,4284],[3570,3570],[2040,2040],[24,24],[336,336],[2184,2184],[8736,8736],[24024,24024],[48048,48048],[72072,72072],[82368,82368],[31,31],[558,558],[4743,4743],[25296,25296],[94860,94860],[265608,265608],[575484,575484],[986544,986544],[38,38],[836,836],[8778,8778],[58520,58520],[277970,277970],[1000692,1000692],[2835294,2835294],[6480672,6480672],[45,45],[1170,1170],[14625,14625],[117000,117000],[672750,672750],[2960100,2960100],[10360350,10360350],[29601000,29601000],[4,4],[12,12],[12,12],[4,4],[0,0],[0,0],[0,0],[0,0],[13,13],[104,104],[364,364],[728,728],[910,910],[728,728],[364,364],[104,104],[22,22],[286,286],[1716,1716],[6292,6292],[15730,15730],[28314,28314],[37752,37752],[37752,37752],[31,31],[558,558],[4743,4743],[25296,25296],[94860,94860],[265608,265608],[575484,575484],[986544,986544],[40,40],[920,920],[10120,10120],[70840,70840],[354200,354200],[1345960,1345960],[4037880,4037880],[9806280,9806280],[49,49],[1372,1372],[18522,18522],[160524,160524],[1003275,1003275],[4815720,4815720],[18460260,18460260],[58017960,58017960],[58,58],[1914,1914],[30624,30624],[316448,316448],[2373360,2373360],[13765488,13765488],[64238944,64238944],[247778784,247778784],[5,5],[20,20],[30,30],[20,20],[5,5],[0,0],[0,0],[0,0],[16,16],[160,160],[720,720],[1920,1920],[3360,3360],[4032,4032],[3360,3360],[1920,1920],[27,27],[432,432],[3240,3240],[15120,15120],[49140,49140],[117936,117936],[216216,216216],[308880,308880],[38,38],[836,836],[8778,8778],[58520,58520],[277970,277970],[1000692,1000692],[2835294,2835294],[6480672,6480672],[49,49],[1372,1372],[18522,18522],[160524,160524],[1003275,1003275],[4815720,4815720],[18460260,18460260],[58017960,58017960],[60,60],[2040,2040],[33660,33660],[359040,359040],[2782560,2782560],[16695360,16695360],[80694240,80694240],[322776960,322776960],[71,71],[2840,2840],[55380,55380],[701480,701480],[6488690,6488690],[46718568,46718568],[272524980,272524980],[1323692760,1323692760],[6,6],[30,30],[60,60],[60,60],[30,30],[6,6],[0,0],[0,0],[19,19],[228,228],[1254,1254],[4180,4180],[9405,9405],[15048,15048],[17556,17556],[15048,15048],[32,32],[608,608],[5472,5472],[31008,31008],[124032,124032],[372096,372096],[868224,868224],[1612416,1612416],[45,45],[1170,1170],[14625,14625],[117000,117000],[672750,672750],[2960100,2960100],[10360350,10360350],[29601000,29601000],[58,58],[1914,1914],[30624,30624],[316448,316448],[2373360,2373360],[13765488,13765488],[64238944,64238944],[247778784,247778784],[71,71],[2840,2840],[55380,55380],[701480,701480],[6488690,6488690],[46718568,46718568],[272524980,272524980],[1323692760,1323692760],[84,84],[3948,3948],[90804,90804],[1362060,1362060],[14982660,14982660],[128850876,128850876],[901956132,901956132],[5282885916,5282885916]]

h, w = map(int, input().split())

def load_map():
    ret = []
    for i in range(h):
        ret.append(input())
    return ret

def map_parity(m):
    p = 0
    for y in range(h):
        for x in range(w):
            if m[y][x] == 'O':
                p += x + y
    return p % 2

def pawn_count(m):
    return sum(1 for x in "".join(m) if x == 'O')

start_map = load_map()
input() # Skip empty line
end_map = load_map()

start_parity = map_parity(start_map)
end_parity = map_parity(end_map)

if start_parity != end_parity:
    print("0")
    sys.exit(0)

# Compute the number of moves that can be done from the target
move_count = 0
for y in range(h):
    for x in range(w):
        if end_map[y][x] == '.':
            continue
        
        if x > 0 and end_map[y][x - 1] == '.':
            move_count += 1
        if x < w - 1 and end_map[y][x + 1] == '.':
            move_count += 1
        if y > 0 and end_map[y - 1][x] == '.':
            move_count += 1
        if y < h - 1 and end_map[y + 1][x] == '.':
            move_count += 1

decimal.getcontext().prec = 100

prob = decimal.Decimal(move_count) / decimal.Decimal(PRECOMP[(pawn_count(end_map) - 1) + 8 * ((h - 1) + 6 * (w - 1))][end_parity])

print("{:f}".format(prob.quantize(decimal.Decimal("0.000000000000001"))))