import Control.Applicative ((<$>)) import Control.Monad (forM_) import Data.Vector.Unboxed (Vector, (!), (//)) import qualified Data.Vector.Unboxed as V import Text.Printf main :: IO () main = do n <- readLn printV n $ solve n solve :: Int -> Vector Int solve n = f 1 (V.replicate (n*n) 0) (0,0) drs where drs = cycle [(0,1),(1,0),(0,-1),(-1,0)] f x v (i, j) ((cdi, cdj):(ndi, ndj):rs) | x > n * n = v | i == n || j == n || i == (-1) || j == (-1) || v ! (n*i+j) > 0 = f x v (i-cdi+ndi, j-cdj+ndj) ((ndi, ndj):rs) | otherwise = f (x+1) (v // [(n*i+j, x)]) (i+cdi,j+cdj) ((cdi, cdj):(ndi, ndj):rs) printV :: Int -> Vector Int -> IO () printV n v = do forM_ [0 .. n-1] $ \i -> do forM_ [0 .. n-1] $ \j -> do if j == 0 then printf "%03d" (v ! (n * i + j)) else if j == n - 1 then printf " %03d\n" (v ! (n * i + j)) else printf " %03d" (v ! (n * i + j))