By "connected," I assume that you mean the adjacent: that is, (5,3,1234) and (4,3,1234) will be connected.
Thus, what you can do is double connect the table to yourself, where each connection depends on the one that preceded it, and the conditions include:
on nextPiece.type = lastPiece.type and (nextPiece.x in (lastPiece.x - 1, lastPiece.x + 1) or nextPiece.y in (lastPiece.x - 1, lastPiece.x + 1))
Note that this does not consider diagonals adjacent.
The problem with this method is that it will return duplicates: if record A is connected to record B, then both A and B will be displayed in the result set. When you join twice, you will see three duplicates ... You can do select distinct , if all you are interested in is a coincidence, but the query will not be particularly fast at all anyway (depending on how big your grid is and how little it is full).
EDIT See Braiba's solution (and comments below): I made a mistake: P
source share