import qualified Data.ByteString.Char8 as C
import Data.Maybe
import qualified Data.Vector as V
import Data.Vector.Unboxed
import Prelude hiding (drop, replicate, reverse, splitAt, toList, unfoldr, (++))

main = C.interact $ put . sol . get

get = unfoldr (C.readInt . C.dropWhile (<'0'))

put :: V.Vector (Vector Int) -> C.ByteString
put = C.unlines . V.toList . V.map (C.unwords . fmap (C.pack . show) . toList)

sol v = u
  where
  h = v!0
  w = v!1
  u = V.imap (\i a -> if odd i then reverse a else a)
    . V.unfoldrN h (Just . splitAt w)
    . ifoldl' (\u i a -> u ++ replicate a (i+1)) empty
    $ drop 3 v