Magic Square

Time limit2sMemory limit128 MB

Summary
Construct and print any N by N magic square for a given N between 3 and 300 using integers 1 through N^2.
Level

Medium7 of 10

Topics
Math, Matrix, Implementation, Simulation
Solved
No attempts yet

Problem

A magic square is an arrangement of the integers from 1 through N^2 in an N by N grid, using each integer exactly once, such that every row, every column, and the two main diagonals all have the same sum.

Given N, print any N by N magic square.

Input

The first line contains a natural number N. (3 <= N <= 300)

Output

Print any N by N magic square that satisfies the condition. Print N integers on each line, separated by spaces.

Examples1

  1. Example 1

    Input
    5
    
    Expected output
    17 24 1 8 15 
    23 5 7 14 16 
    4 6 13 20 22 
    10 12 19 21 3 
    11 18 25 2 9