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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    cout.precision(17);
    cout << fixed;

    int X, Y;
    cin >> Y >> X;

    int BPawnsQ, WPawnsQ, PawnsQ;
    BPawnsQ = WPawnsQ = 0;

    // Start board
    for (int y = 0; y < Y; y++)
        for (int x = 0; x < X; x++)
        {
            char In;
            cin >> In;

            if (In == 'O')
            {
                if (x % 2 == y % 2)
                    BPawnsQ++;
                else
                    WPawnsQ++;
            }

        }
    PawnsQ = BPawnsQ + WPawnsQ;

    int TarBPawnsQ, TarWPawnsQ;
    TarBPawnsQ = TarWPawnsQ = 0;
    vector<int> TarPawns;

    // Target board
    for (int y = 0; y < Y; y++)
        for (int x = 0; x < X; x++)
        {
            char In;
            cin >> In;

            if (In == 'O')
            {
                if (x % 2 == y % 2)
                    TarBPawnsQ++;
                else
                   TarWPawnsQ++;

                if ((x == 0 || x == X - 1) && (y == 0 || y == Y - 1))
                    TarPawns.emplace_back(2);
                else
                if (x == 0 || x == X - 1 || y == 0 || y == Y - 1)
                    TarPawns.emplace_back(3);
                else
                    TarPawns.emplace_back(4);
            }
        }

    if (BPawnsQ % 2 == TarBPawnsQ % 2)
    {
        double Result = 0.0;
        int Denominator = 4 * X * Y - 2 * X - 2 * Y;

        for (int i0 = 0; i0 < PawnsQ; i0++)
        {
            if (PawnsQ == 1)
                Result += TarPawns[i0] / (double) Denominator;
            else
                for (int i1 = 0; i1 < PawnsQ; i1++)
                    if (PawnsQ == 2 && i1 != i0)
                    {
                        double Den = Denominator;

                        double r = TarPawns[i0] / Den;
                        Den -= TarPawns[i0];
                        r *= TarPawns[i1] / Den;

                        Result += r;
                    } else
                        for (int i2 = 0; i2 < PawnsQ; i2++)
                            if (PawnsQ == 3 && i2 != i0 && i2 != i1)
                            {
                                double Den = Denominator;

                                double r = TarPawns[i0] / Den;
                                Den -= TarPawns[i0];
                                r *= TarPawns[i1] / Den;
                                Den -= TarPawns[i1];
                                r *= TarPawns[i2] / Den;

                                Result += r;
                            } else
                                for (int i3 = 0; i3 < PawnsQ; i3++)
                                    if (PawnsQ == 4 && i3 != i0 && i3 != i1 && i3 != i2)
                                    {
                                        double Den = Denominator;

                                        double r = TarPawns[i0] / Den;
                                        Den -= TarPawns[i0];
                                        r *= TarPawns[i1] / Den;
                                        Den -= TarPawns[i1];
                                        r *= TarPawns[i2] / Den;
                                        Den -= TarPawns[i2];
                                        r *= TarPawns[i3] / Den;

                                        Result += r;
                                    } else
                                        for (int i4 = 0; i4 < PawnsQ; i4++)
                                            if (PawnsQ == 5 && i4 != i0 && i4 != i1 && i4 != i2 && i4 != i3)
                                            {
                                                double Den = Denominator;

                                                double r = TarPawns[i0] / Den;
                                                Den -= TarPawns[i0];
                                                r *= TarPawns[i1] / Den;
                                                Den -= TarPawns[i1];
                                                r *= TarPawns[i2] / Den;
                                                Den -= TarPawns[i2];
                                                r *= TarPawns[i3] / Den;
                                                Den -= TarPawns[i3];
                                                r *= TarPawns[i4] / Den;

                                                Result += r;
                                            } else
                                                for (int i5 = 0; i5 < PawnsQ; i5++)
                                                    if (PawnsQ == 6 && i5 != i0 && i5 != i1 && i5 != i2 && i5 != i3 && i5 != i4)
                                                    {
                                                        double Den = Denominator;

                                                        double r = TarPawns[i0] / Den;
                                                        Den -= TarPawns[i0];
                                                        r *= TarPawns[i1] / Den;
                                                        Den -= TarPawns[i1];
                                                        r *= TarPawns[i2] / Den;
                                                        Den -= TarPawns[i2];
                                                        r *= TarPawns[i3] / Den;
                                                        Den -= TarPawns[i3];
                                                        r *= TarPawns[i4] / Den;
                                                        Den -= TarPawns[i4];
                                                        r *= TarPawns[i5] / Den;

                                                        Result += r;
                                                    } else
                                                        for (int i6 = 0; i6 < PawnsQ; i6++)
                                                            if (PawnsQ == 7 && i6 != i0 && i6 != i1 && i6 != i2 && i6 != i3 && i6 != i4 && i6 != i5)
                                                            {
                                                                double Den = Denominator;

                                                                double r = TarPawns[i0] / Den;
                                                                Den -= TarPawns[i0];
                                                                r *= TarPawns[i1] / Den;
                                                                Den -= TarPawns[i1];
                                                                r *= TarPawns[i2] / Den;
                                                                Den -= TarPawns[i2];
                                                                r *= TarPawns[i3] / Den;
                                                                Den -= TarPawns[i3];
                                                                r *= TarPawns[i4] / Den;
                                                                Den -= TarPawns[i4];
                                                                r *= TarPawns[i5] / Den;
                                                                Den -= TarPawns[i5];
                                                                r *= TarPawns[i6] / Den;

                                                                Result += r;
                                                            } else
                                                                for (int i7 = 0; i7 < PawnsQ; i7++)
                                                                    if (PawnsQ == 8 && i7 != i0 && i7 != i1 && i7 != i2 && i7 != i3 && i7 != i4 && i7 != i5 && i7 != i6)
                                                                    {
                                                                        double Den = Denominator;

                                                                        double r = TarPawns[i0] / Den;
                                                                        Den -= TarPawns[i0];
                                                                        r *= TarPawns[i1] / Den;
                                                                        Den -= TarPawns[i1];
                                                                        r *= TarPawns[i2] / Den;
                                                                        Den -= TarPawns[i2];
                                                                        r *= TarPawns[i3] / Den;
                                                                        Den -= TarPawns[i3];
                                                                        r *= TarPawns[i4] / Den;
                                                                        Den -= TarPawns[i4];
                                                                        r *= TarPawns[i5] / Den;
                                                                        Den -= TarPawns[i5];
                                                                        r *= TarPawns[i6] / Den;
                                                                        Den -= TarPawns[i6];
                                                                        r *= TarPawns[i7] / Den;

                                                                        Result += r;
                                                                    }
        }

        cout << Result << "\n";
    }
    else
        cout << 0 << "\n";

    return 0;
}