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
#include<cstdio>
typedef long long ll;
const int N=8;
int n,m,o,i,j,k,u,S,cur,odda,oddb,cnta,cntb,deg;
char a[N+5][N+5],b[N+5][N+5];
ll f[2][(1<<N)+1][N+1][2],g[2][(1<<N)+1][N+1][2],sum;
int main(){
  scanf("%d%d",&n,&m);
  for(i=0;i<n;i++)scanf("%s",a[i]);
  for(i=0;i<n;i++)scanf("%s",b[i]);
  for(i=0;i<n;i++)for(j=0;j<m;j++){
    if(a[i][j]=='O')odda^=(i+j)&1,cnta++;
    if(b[i][j]=='O'){
      oddb^=(i+j)&1,cntb++;
      if(i&&b[i-1][j]=='.')deg++;
      if(i+1<n&&b[i+1][j]=='.')deg++;
      if(j&&b[i][j-1]=='.')deg++;
      if(j+1<m&&b[i][j+1]=='.')deg++;
    }
  }
  if(odda!=oddb||cnta!=cntb)return puts("0"),0;
  f[0][0][0][0]=1;
  for(i=0;i<n;i++)for(j=0;j<m;j++){
    for(S=0;S<1<<m;S++)for(k=0;k<=cnta;k++)for(u=0;u<2;u++){
      f[o^1][S][k][u]=0;
      g[o^1][S][k][u]=0;
    }
    for(S=0;S<1<<m;S++)for(k=0;k<=cnta;k++)for(u=0;u<2;u++){
      ll F=f[o][S][k][u],G=g[o][S][k][u];
      if(!F)continue;
      int up=i?(S>>j&1):0;
      int left=j?(S>>(j-1)&1):0;
      for(cur=0;cur<2;cur++){
        int nS=S^((up^cur)<<j);
        int nk=k+cur;
        if(nk>cnta)continue;
        int nu=u;
        if(cur)nu^=(i+j)&1;
        int d=0;
        if(i)d+=up^cur;
        if(j)d+=left^cur;
        f[o^1][nS][nk][nu]+=F;
        g[o^1][nS][nk][nu]+=G+d*F;
      }
    }
    o^=1;
    /*for(S=0;S<1<<m;S++)for(k=0;k<=cnta;k++)for(u=0;u<2;u++){
      ll F=f[o][S][k][u],G=g[o][S][k][u];
      if(F)printf("i=%d j=%d S=%d k=%d u=%d F=%lld G=%lld\n",i,j,S,k,u,F,G);
    }*/
  }
  for(S=0;S<1<<m;S++)sum+=g[o][S][cnta][odda];
  //printf("%d/%lld",deg,sum);
  printf("%.15f",((double)deg/(double)sum));
}