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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

LL n,m;
bool P[20][20];     // board
LL exp2_[10];       // exponents of 2
LL moveCount[256];  // number of moves for each column distribution - indexed by bit code (8 bits)
LL pieceCount[256]; // number of pieces for a code (i.e. number of 1s in a bit code
LL commonBits[256][256];  // number of common bits between 2 codes

/// Reads data from input stream to global variable P
void readBoard()
{
    string line;
    FOR(i,1,n)
    {
        cin >> line;
        if (line.length()==0) continue;
        FOR(j,1,m) P[i][j]=(line[j-1]!='.');
    }
}

/// Reads part of input data
void readData()
{
    cin >> n; cin >> m;
    readBoard();
}

void showBoard()
{
    FOR(i,1,n)
    {
        FOR(j,1,m) if (P[i][j]) cout << "#"; else cout << ".";
        cout << "\n";
    }
}

/// Counts possible moves for board stored in P
LL possibleMoves()
{
    LL moves=0;
    FOR(i,1,n) FOR(j,1,m) if (P[i][j])
    {
        if (i>1 && !P[i-1][j]) moves++;
        if (i<n && !P[i+1][j]) moves++;
        if (j>1 && !P[i][j-1]) moves++;
        if (j<m && !P[i][j+1]) moves++;
    }
    return moves;
}

bool roz[10];
/// Calculates static arrays moveCount and pieceCount
void calculateForOneColumn()
{
    FOR(r,0,exp2_[n]-1)
    {
        LL rr=r;
        FOR(i,1,n)
        {
            roz[i]=((rr%2)==1);
            rr=rr/2;
        }
        LL ileM=0; LL ileP=0;
        FOR(i,1,n) if (roz[i])
        {
            ileP++;
            if ((i>1)&&(!roz[i-1])) ileM++;
            if ((i<n)&&(!roz[i+1])) ileM++;
        }
        moveCount[r]=ileM;
        pieceCount[r]=ileP;
    }
}

/// Main value of whole algoritm:
/// C[i][j][k][r] = number of states, which have i possible moves, are on j columns [1..j],
/// have k pieces on board and last column has piece distribution coded by r
LL C[40][9][9][256];

/// Calculates array C by dynamic programming
void calculateC()
{
    // initialize whole array to 0:
    FOR(i,0,39)FOR(j,0,8)FOR(k,0,8)FOR(r,0,255) C[i][j][k][r]=0;

    // calculate first column:
    FOR(r,0,exp2_[n]-1) C[moveCount[r]][1][pieceCount[r]][r]+=1;

    // calculate other columns:
    FOR(j,2,8)
    {
        FOR(rl,0,exp2_[n]-1)
        {
            LL piecesLast=pieceCount[rl];
            LL movesVertLast=moveCount[rl];
            FOR(r,0,exp2_[n]-1)
            {
                LL newHorizontalMoves=pieceCount[r]+pieceCount[rl]-2*commonBits[r][rl];
                LL newMovesTotal=newHorizontalMoves+movesVertLast;
                FOR(i,0,39)FOR(k,0,8-piecesLast) if (C[i][j-1][k][r]!=0)
                {
                    C[i+newMovesTotal][j][k+piecesLast][rl]+=C[i][j-1][k][r];
                }
            }
        }
    }
}

/// Calculates static array commonBits[r1][r2]
void calculateCommonBits()
{
    FOR(r1,0,255)FOR(r2,0,255)
    {
        LL comm=0;
        FOR(i,0,7) if (((r1&exp2_[i])!=0)&&((r2&exp2_[i])!=0)) comm++;
        commonBits[r1][r2]=comm;
    }
}

/// Calculates total number of all possible moves of all possible states
/// for j columns and k pieces
LL calculateTotal(int j, int k)
{
    LL total=0;
    FOR(i,0,39)FOR(r,0,exp2_[n]-1) total+=i*C[i][j][k][r];
    return total;
}

/// Calculates parity of state of board P
LL calculateParity()
{
    LL total=0;
    FOR(i,1,n)FOR(j,1,m) if (P[i][j]) total+=(i+j);
    return total%2;
}

/// Calculates number of pieces on board P
LL piecesOnBoard()
{
    LL p=0;
    FOR(i,1,n)FOR(j,1,m) if (P[i][j]) p++;
    return p;
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	exp2_[0]=1; FOR(i,1,9) exp2_[i]=2*exp2_[i-1];
	calculateCommonBits();

    readData();
	calculateForOneColumn();
	calculateC();
    LL par1=calculateParity();
    readBoard();
    LL par2=calculateParity();
    if (par1!=par2) { cout << "0\n"; return 0; }
    LL tot=calculateTotal(m,piecesOnBoard());
    double licznik=2*possibleMoves();
    double mianownik=tot;
    cout.precision(17);
    cout << fixed << licznik/mianownik << "\n";
	return 0;
}