Find neighbouring fields in a matrix

hi there,
I have a 5x5 matrix of squares. (25 fields) in this matrix I want to show me all the possible options of patterns that come out, if I define 10 squares that only showif they have at least one adjacent/neighboring square. (so I only want to have patterns of 10 connected areas).

do you have any ideas or tips of how to set up the “adjacent” rule, that I could then plug it into Galapagos or so?

thank yo in advance

 Do you mean this index map?   

 [ i - 1, j + 1 ]    [ i, j + 1 ]    [ i + 1, j + 1 ]
 [ i - 1, j     ]    [ i, j     ]    [ i + 1, j     ]  
 [ i - 1, j - 1 ]    [ i, j - 1 ]    [ i + 1, j - 1 ] 

 This version has the boundaries wrapped:

 [ (i-1+U)%U, (j+1)%V   ]    [ i, (j+1)%V   ]    [ (i+1)%U, (j+1)%V ]
 [ (i-1+U)%U,  j        ]    [ i,  j        ]    [ (i+1)%U, j       ]  
 [ (i-1+U)%U, (j-1-V)%V ]    [ i, (j-1-V)%V ]    [ (i+1)%U, (j-1)%V ]

Hi Dani,

thank you for your reply. here is a picture of the outcome, that i am trying to achieve. i want to set a grid in which i want to randomly create patterns of tiles to identify each room with a unique pattern.

so i was thinking to set up a grid (5x5 or 9x9 or ?x?) in this grid i wanted to select Xnumber random fields to form a pattern. But the pattern is only “good” if it is a coninous pattern of connected areas… So if a pattern is formed with some fields not having any neigbouring filed, is “no good”…?

I hope the image shows the idea a little bit better…