Rectangle Islands (Rectangle Grids)

Auxs

CI[R_,T2_]:=Map[{#,Take[R,Sequence@@(Transpose[#])]}&,T2]

TakingMin[P_,Q_]:=Min[Flatten[Take[P,Sequence@@Transpose[Q]]]]

Clear[TakingNeigbM]

TakingNeigbM[P_,Q_,n_,m_]:=Module[{T},T=Join[Table[{i,Q[[1,2]]-1},{i,Q[[1,1]]-1,Q[[2,1]]+1}],Table[{i,Q[[2,2]]+1},{i,Q[[1,1]]-1,Q[[2,1]]+1}],Table[{Q[[1,1]]-1,j},{j,Q[[1,2]],Q[[2,2]]}],Table[{Q[[2,1]]+1,j},{j,Q[[1,2]],Q[[2,2]]}]];
T=Select[T,(1<=#[[1]]≤n∧1≤#[[2]]≤m)&];
Max@@Map[P[[Sequence@@#]]&,T]]

IsIslandQ[P_,Q_,n_,m_]:=TakingMin[P,Q]>TakingNeigbM[P,Q,n,m]

Main

NoOfIslands2[T_List]:=Module[{n,m,A,B},{n,m}=Dimensions[T];
A=Flatten[Table[{i,j},{i,n},{j,m}],1];
B=Flatten[Table[{A[[i]],A[[j]]},{i,n m},{j,n m}],1];
B=Select[B,(#[[1,1]]≤#[[2,1]]∧#[[1,2]]≤#[[2,2]]&)];
Plus@@Map[IsIslandQ[T,#,n,m]&,B]/.{True->1,False->0}
]

TwoDIslands[T_List]:=Module[{n,m,A,B},{n,m}=Dimensions[T];
A=Flatten[Table[{i,j},{i,n},{j,m}],1];
B=Flatten[Table[{A[[i]],A[[j]]},{i,n m},{j,n m}],1];
B=Select[B,(#[[1,1]]≤#[[2,1]]∧#[[1,2]]≤#[[2,2]]&)];
Select[B,IsIslandQ[T,#,n,m]&]
]

ShowConfig[Is_]:=Module[{g1,rn,cn},{rn,cn}=Dimensions[Is];g1=Table[Cuboid[{j,i,0},{j+1,i+1,Is[[j,i]]}],{i,cn},{j,rn}]]

ComputeMax[ic_]:=Module[{n,m},{n,m}=Dimensions[ic];Floor[(n m+m+n-1)/2]]

Two prototypes

config1={{2,1,2},{3,1,3}};

config2={{2,1,1,2,3},{2,3,4,5,1},{1,2,4,4,2}};

Flatten[config1]

Dimensions[config1]

Dimensions[config2]

second row
first row

Grid[config1,Frame->All]

Grid[config2,Frame->All]

NoOfIslands2[config1]

NoOfIslands2[config2]

TwoDIslands[config1]

TwoDIslands[config2]

x=Table[1,{2},{3}];

Experiments

Manipulate[Column[{Panel["1st Game  --  Islands   :   Reach   the   Maximum !",Button["Init",x:=Table[1,{m},{n}] ]Checkbox[NoOfIslands2[x]==ComputeMax[x]]],InputField[NoOfIslands2[x]],InputField[ComputeMax[x]],Graphics3D[ShowConfig[x],Boxed->False],EventHandler[GraphicsGrid[Join[Table[Annotation[Graphics[{Disk[]}],{j,i},"Mouse"],{j,m},{i,n}]],Frame->All],{"MouseDown":>(a=MouseAnnotation[];x=ReplacePart[x,a->x[[Sequence@@a]]+If[s==0,-1,1]])}]}],{{m,2,"m"},2,5,1,Appearance->"Labeled"},{{n,3,"n"},3,5,1,Appearance->"Labeled"},{{s,1,"+/-"},0,1,1,ImageSize->Tiny},Initialization:>{x:=Table[1,{2},{3}];},SaveDefinitions->True]