For each mask, enumerate all ways to fill the row. This is recursive: at each column, try placing or not placing. Recursive generator: explores placing horizontal, vertical, or nothing at column . improvement: precompute all valid (mask, nextMask) pairs.
Store in adjacency list. DP becomes matrix multiplication. For large , small : matrix exponentiation gives . Useful when .