Magic Grid
Let us define a magic grid to be a square matrix of integers of size $$$n \times n$$$, satisfying the following conditions.   All integers from $$$0$$$ to $$$(n^2 - 1)$$$ inclusive appear in the matrix exactly once.  Bitwise XOR of all elements in a row or a column must be the same for each row and column. You are given an integer $$$n$$$ which is a multiple of $$$4$$$. Construct a magic grid of size $$$n \times n$$$.
In the first example, XOR of each row and each column is $$$13$$$.In the second example, XOR of each row and each column is $$$60$$$.
The only line of input contains an integer $$$n$$$ ($$$4 \leq n \leq 1000$$$). It is guaranteed that $$$n$$$ is a multiple of $$$4$$$.
Print a magic grid, i.e. $$$n$$$ lines, the $$$i$$$-th of which contains $$$n$$$ space-separated integers, representing the $$$i$$$-th row of the grid.If there are multiple answers, print any. We can show that an answer always exists.
n = int(input())
for i in range(0, n, 4):
    print(' '.join((str(j) for j in range(2 * (i // 4) * n, 2 * (i // 4) * n + 2 * n, 2))))
    print(' '.join((str(j) for j in range(n * n - 2 * ((i + 1) // 4) * n - 1, n * n - 2 * ((i + 1) // 4) * n - 2 * n, -2))))
    print(' '.join((str(j) for j in range(2 * ((i + 2) // 4) * n + 1, 2 * ((i + 2) // 4) * n + 2 * n + 1, 2))))
    print(' '.join((str(j) for j in range(n * n - 2 * ((i + 3) // 4) * n - 2, n * n - 2 * ((i + 3) // 4) * n - 2 * n - 2, -2))))
a = int(input())
for i in range(a):
    A = []
    m = i // 4
    k = i % 4
    for j in range(a):
        A.append(str(4 * a * m + j * 4 + k))
    print(' '.join(A))
nice = int(input())
for i in range(nice):
    result = list()
    (mango, k) = (i // 4, i % 4)
    for j in range(nice):
        result.append(str(4 * nice * mango + 4 * j + k))
    print(' '.join(result))