"The permanent"

Per[m_List]:=With[{v=Array[x,Length[m]]},Coefficient[Times@@(m.v),Times@@v]];

"B computes the board corresponding to the grid graph for an m by kk array"

B[m_,kk_]:=Module[{K=kk*m/2,i,c=1,d=1,s=0,A,k},If[Mod[m*kk,2]≠0,Return[Error],A=0*IdentityMatrix[K]];While[c≤K&&d≤K,k=1;While[k<m-1,If[c≤K&&d≤K,A[[c]][[d]]=1,Break[];];c=c+s*(1-Mod[k,2])+(1-s)*Mod[k,2];d=d+(1-s)*(1-Mod[k,2])+s*Mod[k,2];k=k+1;];If[c≤K&&d≤K,A[[c]][[d]]=1,Break[];];c=c+1;d=d+1;If[s==0,s=1,s=0];];If[Mod[m,2]==0,For[i=1,i<=K-m/2,i++,A[[i]][[i+m/2]]=1;A[[i+m/2]][[i]]=1;],For[i=1,i<=K-(m-1)/2,i++,A[[i]][[i+(m-1)/2]]=1;];For[i=1,i<=K-(m+1)/2,i++,A[[i+(m+1)/2]][[i]]=1;]];Return[A];]

"PR counts perfect matchings for the 'complement' (i.e. including the edges of the complete graphs on the white, and independently, black vertex sets) of a (subgraph of a) grid graph. Here A is the literal complement board of the (subset of the) grid graph alone."

PR[A_]:=Module[{k},If[A=={{0}},Return[0];];k=Length[A];If[Mod[k,2]==0,Return[Sum[CPM[A,2*i]*((k-2*i-1) !!)^2,{i,0,k/2}]],Return[Sum[CPM[A,2*i+1]*((k-2*i-2) !!)^2,{i,0,(k-1)/2}]]]]

"CPM calculates the kth rook number for the board A"

CPM[A_,k_]:=Module[{h,t,r,i,res=0,j},If[k==0,Return[1]];If[k==1,h=Flatten[A];Return[Sum[h[[i]],{i,1,Length[h]}]]];t=Table[i,{i,1,Length[A]}];r=Subsets[t,{k}];For[i=1,i≤Length[r],i++,For[j=1,j≤Length[r],j++,res=res+Per[Transpose[A[[r[[i]]]]][[r[[j]]]]]]];Return[res];]

"NN computes the number of p-dominoe configurations in the m by k game of memory"

NN[m_,k_,p_]:=Module[{res=0,R,i,t,j},If[Mod[m*k,2]≠0,Return[Error]];If[p==k*m/2,Return[Per[B[m,k]]]];If[p==0,Return[PR[1-B[m,k]]]];t=Table[i,{i,1,k*m/2}];r=Subsets[t,{p}];R=B[m,k];For[i=1,i≤Length[r],i++,For[j=1,j<=Length[r],j++,res=res+Per[Transpose[R[[r[[i]]]]][[r[[j]]]]]*PR[1-Transpose[R[[Complement[t,r[[i]]]]]][[Complement[t,r[[j]]]]]];]];Return[res];]