Find a n x m binary matrix with exactly a 1's in all rows and b 1's in all columns
You don't need to collect the rows in a matrix because you only touch each row once, and you don't need Data.Array because you only ever process a whole row. The check at the end makes the checks in the middle functionally superfluous. mapAccumL captures the explicit recursion.

main :: IO ()
main = do
  t &lt;- readLn
  replicateM_ t $ do
    [n,m,a,b] &lt;- map read . words &lt;$&gt; getLine
    mapM putStrLn $ case solve n m a of
      (colcnts, rows) | all (==b) colcnts -&gt; "YES" : map (concatMap show) rows
      _ -&gt; ["NO"]

solve :: Int -&gt; Int -&gt; Int -&gt; ([Int], [[Int]])
solve n m a = mapAccumL go (replicate m 0) [1..n] where
  go colcnts c = (zipWith (+) row colcnts, row) where row
    = map fst $ sortOn snd $ zip (replicate a 1 ++ repeat 0)
    $ map fst $ sortOn snd $ zip [1..] colcnts


Note that your entire approach doesn't obviously find a solution if there is one. Can you prove that?

Edit: I thought you process each row at a time. Easy to mix up since the approaches are symmetric :). I choose the row-at-a-time approach, since they can be directly printed.