import Data.List.Split
import Data.Char

main = fmap (map read . words) getLine >>=
  \[_,w] -> getLine >>=
  \_ -> fmap (map read . words ) getLine >>=
  \as -> let xs = (chunksOf w . concatMap (\(x,y) -> replicate y (show x)) . zip [1..]) as
         in mapM_ putStrLn $ map concat $ map g (f 1 xs)

-- [[1,2,2],[3,3,3],[4,4,4],[4,5,5],[5,5,5]]

f :: Int -> [[a]] -> [[a]]
f _ [] = []
f n (xs:xss)
  | odd n = xs : f (n+1) xss
  | otherwise = reverse xs : f (n+1) xss

g :: [String] -> [String]
g [] = []
g (x:xs) = x : " " : g xs