Let us define a magic grid to be a square matrix of integers of size n × n, satisfying the following conditions. 

  * All integers from 0 to (n^2 - 1) inclusive appear in the matrix exactly once. 
  * [Bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#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 × n.

Input

The only line of input contains an integer n (4 ≤ n ≤ 1000). It is guaranteed that n is a multiple of 4.

Output

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.

Examples

Input


4


Output


8 9 1 13
3 12 7 5
0 2 4 11
6 10 15 14

Input


8


Output


19 55 11 39 32 36 4 52
51 7 35 31 12 48 28 20
43 23 59 15 0 8 16 44
3 47 27 63 24 40 60 56
34 38 6 54 17 53 9 37
14 50 30 22 49 5 33 29
2 10 18 46 41 21 57 13
26 42 62 58 1 45 25 61

Note

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.

Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Starter code:

Solutions:
n = int(input())
for i in range(n):
	printlist = []
	m = i // 4
	k = i % 4
	for j in range(n):
		printlist.append(str(4 * n * m + 4 * j + k))
	print(' '.join(printlist))


def main():
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	count = 0
	for j in range(0, n, 4):
		for i in range(0, n, 4):
			for k in range(4):
				for m in range(4):
					a[i + k][j + m] = count
					count += 1
	for i in a:
		print(*i)
main()


n = int(input())
matrix = [[None] * n for x in range(n)]
binary = bin(n // 4)[2:]
for x in range(2 * 2 ** len(binary)):
	for y in range(2 * 2 ** len(binary)):
		matrix[x][y] = x * 2 * 2 ** len(binary) + y
sum = 2 * 2 ** len(binary)
bigsum = sum ** 2
for i in range(1, len(binary)):
	if binary[i] == '1':
		curr = 2 * 2 ** (len(binary) - i)
		for x in range(sum):
			for y in range(curr):
				matrix[x][sum + y] = bigsum
				bigsum += 1
		for y in range(sum):
			for x in range(curr):
				matrix[sum + x][y] = bigsum
				bigsum += 1
		for x in range(curr):
			for y in range(curr):
				matrix[sum + x][sum + y] = bigsum
				bigsum += 1
		sum += curr
strs = [' '.join(list(map(str, l))) for l in matrix]
print('\n'.join(strs))


n = int(input())
arr = [[0 for i in range(n)] for j in range(n)]
count = 0
for i in range(n // 2):
	for j in range(n // 2):
		arr[i][j] = 4 * count + 1
		arr[i + n // 2][j] = 4 * count + 2
		arr[i][j + n // 2] = 4 * count + 3
		arr[i + n // 2][j + n // 2] = 4 * count
		count += 1
for i in arr:
	print(*i)


from sys import stdin, stdout
n = int(stdin.readline().strip())
x = 0
y = 0
acum = 0
mt = [[0 for i in range(n)] for j in range(n)]
for i in range(n // 4 * (n // 4)):
	for j in range(4):
		for k in range(4):
			mt[x + j][y + k] = acum
			acum += 1
	y += 4
	if y == n:
		y = 0
		x += 4
for i in range(n):
	for j in range(n):
		stdout.write('%d ' % mt[i][j])
	stdout.write('\n')


import sys
n = int(input())
_ = []
for i in range(n):
	k = int(i % n / 4)
	_.append(i + (n - 1) * 4 * k)

def fun(mass, d=0):
	for (i, j) in enumerate(mass):
		if (i + 1) % n != 0:
			print(f'{j + d}', end=' ')
		else:
			print(f'{j + d}', end='\n')
for i in range(n):
	fun(_, i * 4)


def generate(n):
	mat = [[0 for _ in range(n)] for _ in range(n)]
	num = 0
	rev = False
	for i in range(0, n, 2):
		for j in range(0, n, 2):
			mat[i][j] = num
			mat[i][j + 1] = num + 1
			mat[i + 1][j] = num + 2
			mat[i + 1][j + 1] = num + 3
			if rev:
				(mat[i][j], mat[i + 1][j]) = (mat[i + 1][j], mat[i][j])
				(mat[i][j + 1], mat[i + 1][j + 1]) = (mat[i + 1][j + 1], mat[i][j + 1])
			num += 4
			rev = not rev
	return mat
n = int(input())
mat = generate(n)
for row in mat:
	for num in row:
		print(num, end=' ')
	print()


import sys

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
n = ri(1)
mat = [[0 for i in range(n)] for j in range(n)]
bits = [0 for i in range(21)]
cnt = 0
for i in range(0, n, 4):
	for j in range(0, n, 4):
		for p in range(4):
			for q in range(4):
				mat[i + p][j + q] = cnt
				cnt += 1
for i in mat:
	print(*i)
size = n * n


N = int(input())
A = [[0 for _ in range(N)] for _ in range(N)]
count = 0
for i in range(0, N, 4):
	for j in range(0, N, 4):
		for di in range(4):
			for dj in range(4):
				A[i + di][j + dj] = count
				count += 1
for i in range(N):
	print(*A[i])


n = int(input())
A = [[0] * n for _ in range(n)]
for i in range(n):
	for j in range(n):
		A[i][j] = i % 4 * 4 + j % 4 + i // 4 * 4 * n + j // 4 * 16
for u in A:
	print(*u)


a = int(input())
for i in range(a):
	for j in range(a):
		x = i << 2
		print(x + j % 4 + 4 * a * (j // 4), end=' ')
	print()


n = int(input()) // 2
a = [[0] * (2 * n) for i in range(2 * n)]
num = 0
for i in range(n):
	for j in range(n):
		a[i][j] = a[i + n][j] = a[i][j + n] = a[i + n][j + n] = num * 4
		a[i][j] += 1
		a[i + n][j] += 2
		a[i][j + n] += 3
		num += 1
for i in range(n * 2):
	print(*a[i])


n = int(input())
gap = 4 * n
a = [[-1] * n for _ in range(n)]
for j in range(n):
	base = gap * (j // 4) + j % 4
	for i in range(n):
		a[i][j] = str(base + i * 4)
for i in range(n):
	print(' '.join(a[i]))


n = int(input())
cnt = n // 4
ans = 0
for _ in range(cnt):
	print(*[ans + i * 4 for i in range(n)])
	print(*[ans + i * 4 + 1 for i in range(n)])
	print(*[ans + i * 4 + 2 for i in range(n)])
	print(*[ans + i * 4 + 3 for i in range(n)])
	ans = ans + n * 4


import math

def solve(n):
	result = []
	for i in range(n):
		result.append([0] * n)
	d0 = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
	start = 0
	for ind0 in range(0, n // 4):
		for ind1 in range(0, n // 4):
			for i in range(4):
				for j in range(4):
					result[ind0 * 4 + i][ind1 * 4 + j] = start + d0[i][j]
			start += 16
	return result

def main():
	n = int(input())
	matrix = solve(n)
	print('\n'.join([' '.join([str(y) for y in x]) for x in matrix]))
main()


n = int(input())
a = [[0 for i in range(n)] for j in range(n)]
dupa = 0
for c in range(n // 4):
	for r in range(n // 4):
		pyk = 0
		for cc in range(4):
			for rr in range(4):
				a[4 * c + cc][4 * r + rr] = dupa + pyk
				pyk += 1
		dupa += 16
for i in range(n):
	print(*a[i])


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
n = rint()
m = [[0] * n for _ in range(n)]
for y in range(0, n, 2):
	for x in range(0, n, 2):
		b = y * n + 2 * x
		m[y][x] = b
		m[y][x + 1] = b + 1
		m[y + 1][x] = b + 3
		m[y + 1][x + 1] = b + 2
for row in m:
	print(' '.join(map(str, row)))


import sys
input = sys.stdin.readline
n = int(input())
A = [[0] * n for i in range(n)]
cnt = 0
for i in range(4):
	for j in range(4):
		A[i][j] = cnt
		cnt += 1
tmp = 0
for i in range(0, n, 4):
	for j in range(0, n, 4):
		for k in range(4):
			for l in range(4):
				A[k + i][l + j] = A[k][l] + tmp
		tmp += 16
for i in A:
	print(*i)


import sys
import copy
input = sys.stdin.readline
n = int(input())
ANS = [[-1] * n for i in range(n)]
now = 0
for i in range(0, n, 4):
	for j in range(0, n, 4):
		score = now
		for k in range(4):
			for l in range(4):
				ANS[i + k][j + l] = score
				score += 1
		now += 16
for ans in ANS:
	print(*ans)


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = 1
while t > 0:
	t -= 1
	n = fi()
	a = [[0] * n for i in range(n)]
	i = 0
	j = 0
	c = 0
	r = [0, 1, 2, 3]
	for i in range(0, n, 2):
		for j in range(0, n, 2):
			for k in range(2):
				for l in range(2):
					a[i + k][j + l] = 4 * c + r[2 * k + l]
			c += 1
	r = [0] * n
	c = [0] * n
	for i in range(n):
		for j in range(n):
			r[i] ^= a[i][j]
			c[j] ^= a[i][j]
	for i in a:
		print(*i)


n = int(input())
c = 0
lis = [[0] * n for i in range(n)]
for k in range(n // 4):
	for i in range(n):
		for j in range(4):
			lis[i][k * 4 + j] = c
			c += 1
for i in range(n):
	print(*lis[i])


import sys
input = sys.stdin.readline
n = int(input())
ans = []
(cur, e, o) = (0, 0, 1)
for i in range(n):
	if i & 1:
		cur = o
	else:
		cur = e
	outp = []
	for j in range(n):
		outp.append(cur)
		cur += 2
	ans.append(outp)
	if i & 1:
		o = cur
	else:
		e = cur
for i in range(n):
	for j in range(n):
		sys.stdout.write(str(ans[j][i]) + ' ')
	sys.stdout.write('\n')


import sys
input = lambda : sys.stdin.readline().strip()
arr = []
n = int(input())
for i in range(n // 4):
	cur = n * 4 * i
	for j in range(4):
		arr.append([])
		for k in range(n):
			arr[-1].append(cur + j + 4 * k)
for i in arr:
	for j in i:
		print(j, end=' ')
	print()


from math import sqrt
n = int(input())
a = [[0] * n for i in range(n)]
num = 0
yy = n ** 2 // 16
yy = int(sqrt(yy))
for q in range(yy):
	for k in range(yy):
		for i in range(q * 4, q * 4 + 4):
			for j in range(k * 4, k * 4 + 4):
				a[i][j] = num
				num += 1
for i in range(n):
	print(*a[i])


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

def li():
	return list(map(int, sys.stdin.readline().split()))

def num():
	return map(int, sys.stdin.readline().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007

def solve():
	t = 1
	for tt in range(t):
		n = nu()
		a = [0] * n
		cc = 0
		i = 0
		j = 0
		if pow(2, int(ma.log2(n))) == n:
			for i in range(n):
				a[i] = [0] * n
				for j in range(n):
					a[i][j] = cc
					cc += 1
			for i in range(n):
				print(*a[i])
			continue
		for i in range(n):
			a[i] = [0] * n
		i = 0
		j = 0
		while i < n:
			j = 0
			while j < n:
				for k in range(i, i + 4):
					for p in range(j, j + 4):
						a[k][p] = cc
						cc += 1
				j += 4
			i += 4
		for i in range(n):
			print(*a[i])
solve()


num = int(input())
ans = [[0] * num for _ in range(num)]
for i in range(num):
	base = i // 2 * num * 2 + i % 2
	for j in range(num):
		ans[i][j] = base + j * 2
for line in ans:
	print(' '.join([str(item) for item in line]))


n = int(input())
aa = [[0 for i in range(n)] for j in range(n)]
cnt = 0
for i in range(0, n, 4):
	for j in range(0, n, 4):
		for ii in range(i, i + 4):
			for jj in range(j, j + 4):
				aa[ii][jj] = cnt
				cnt += 1
for i in range(n):
	for j in range(n):
		if j == n - 1:
			print(aa[i][j])
		else:
			print(aa[i][j], end=' ')


n = int(input())
l = []
for i in range(n):
	k = [0] * n
	l.append(k)
for i in range(1, n):
	l[0][i] = 4 * i
for i in range(1, 4):
	for j in range(n):
		if j == 0:
			l[i][j] = i
		else:
			l[i][j] = l[i][j - 1] + 4
z = l[3][n - 1] + 1
ka = 0
for i in range(4, n):
	for j in range(n):
		if j == 0:
			l[i][j] = z + ka
		else:
			l[i][j] = l[i][j - 1] + 4
	ka += 1
	if (i + 1) % 4 == 0:
		z = l[i][j] + 1
		ka = 0
for i in l:
	print(*i)


n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
c = 0
for j in range(0, n, 4):
	for i in range(0, n, 4):
		for k in range(4):
			for m in range(4):
				A[i + k][j + m] = c
				c += 1
rows = [0 for i in range(n)]
cols = [0 for i in range(n)]
x = 0
for i in range(n):
	x = 0
	for j in range(n):
		x ^= A[i][j]
	rows[i] = x
for i in range(n):
	x = 0
	for j in range(n):
		x ^= A[j][i]
	cols[i] = x
for i in A:
	print(*i)


from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n = int(input())
res = [[0 for i in range(n)] for j in range(n)]

def group(i, j, g):
	if g == 0:
		return (i, j)
	elif g == 1:
		return (i, j + n // 2)
	elif g == 2:
		return (i + n // 2, j)
	elif g == 3:
		return (i + n // 2, j + n // 2)
for i in range(n // 2):
	for j in range(n // 2):
		base = i * (n // 2) + j
		for g in range(4):
			(x, y) = group(i, j, g)
			res[x][y] = 4 * base + g
for row in res:
	print(' '.join(map(str, row)) + '\n')


import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
for y in range(0, n * n, n * 4):
	for x in range(0, n * 4, 16):
		print(*range(x + y + 0, x + y + 4), end=' ')
	print()
	for x in range(0, n * 4, 16):
		print(*range(x + y + 4, x + y + 8), end=' ')
	print()
	for x in range(0, n * 4, 16):
		print(*range(x + y + 8, x + y + 12), end=' ')
	print()
	for x in range(0, n * 4, 16):
		print(*range(x + y + 12, x + y + 16), end=' ')
	print()


zzz = 1
zzz += 1
n = int(input())
ans = [[0 for i in range(n)] for j in range(n)]
ct = 0
for i in range(0, n, 4):
	zzz = 1
	zzz += 1
	for j in range(0, n, 4):
		zzz = 1
		zzz += 1
		for k in range(4):
			zzz ^= 1
			for m in range(4):
				ans[j + k][i + m] = ct
				ct += 0 & zzz
				ct += 1
for i in ans:
	print(*i)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	sup = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
	ans = [[0 for i in range(n)] for j in range(n)]
	m = (n // 4) ** 2
	mx = len(bin(n ** 2 - 1)[2:])
	l = [0 for i in range(m)]
	mx2 = len(bin(m - 1)[2:])
	for i in range(m):
		a = bin(i)[2:]
		a = (mx2 - len(a)) * '0' + a
		l[i] = int(a + '0' * (mx - len(a)), 2)
	ct = 0
	tct = 0
	i_of = 0
	j_of = 0
	offs = []
	for i in range(n // 4):
		for j in range(n // 4):
			offs.append((i, j))
	for ct in range(m):
		for i in range(4):
			for j in range(4):
				ans[offs[ct][0] * 4 + i][offs[ct][1] * 4 + j] = sup[i % 4][j % 4] + l[ct]
	for i in range(n):
		for j in range(n):
			print(int(ans[i][j]), end=' ')
		print()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
l = [[n * j + i for i in range(n)] for j in range(n)]
for i in range(0, n, 2):
	for j in range(0, n, 2):
		(l[i][j], l[i + 1][j + 1]) = (l[i + 1][j + 1], l[i][j])
for i in range(n):
	for j in range(n):
		print(l[i][j], end=' ')
	print()


n = int(input())
x = 0
m = [[-1 for i in range(n)] for i in range(n)]
for i in range(0, n, 4):
	for j in range(0, n, 4):
		for p in range(i, i + 4):
			for q in range(j, j + 4):
				m[p][q] = x
				x += 1
for i in range(n):
	print(*m[i])


import sys
input = sys.stdin.readline
n = int(input())
for y in range(0, n * n, n * 4):
	for a in range(0, 16, 4):
		for x in range(0, n * 4, 16):
			print(x + y + a, x + y + a + 1, x + y + a + 2, x + y + a + 3, end=' ')
		print()


from sys import setcheckinterval, stdin
setcheckinterval(1000)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
n = iin()
a = [[i * 4 + j for j in range(4)] for i in range(4)]
ans = [[0 for i in range(n)] for i in range(n)]
ch = 0
ch1 = n // 4
for i in range((n // 4) ** 2):
	(x, y) = (i // ch1, i % ch1)
	for j in range(4):
		for k in range(4):
			ans[4 * x + j][4 * y + k] = a[j][k] + 16 * ch
	ch += 1
for i in range(n):
	print(*ans[i])


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))


N = int(input())
arr = [[N * j + i for i in range(N)] for j in range(N)]
for i in range(0, N, 2):
	for j in range(0, N, 2):
		(arr[i][j + 1], arr[i + 1][j]) = (arr[i + 1][j], arr[i][j + 1])
for i in arr:
	print(*i)


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))))


n = int(input())
a = [i for i in range(n * n)]
b = [[0 for i in range(n)] for i in range(n)]
t = 0
for i in range(n // 4):
	for j in range(n // 4):
		(x, y) = (0, 0)
		while x < 4:
			y = 0
			while y < 4:
				b[i * 4 + x][j * 4 + y] = a[t]
				t += 1
				y += 1
			x += 1
for i in b:
	print(*i, end=' ')
	print()


from sys import stdin, stdout
from collections import defaultdict
from collections import deque
import math
import copy
N = int(input())
data = [[0] * N for _ in range(N)]
arr = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
for i in range(4):
	for j in range(4):
		data[i][j] = arr[i][j]
X = N // 4
K = 1
for i in range(X):
	for j in range(X):
		if i == 0 and j == 0:
			continue
		u = i * 4
		v = j * 4
		for m in range(4):
			for n in range(4):
				data[u + m][v + n] = arr[m][n] + 16 * K
		K += 1
for i in range(N):
	print(*data[i])


import math, sys
from collections import Counter, defaultdict
from sys import stdin, stdout
input = stdin.readline
lili = lambda : list(map(int, sys.stdin.readlines()))
li = lambda : list(map(int, input().split()))
n = int(input())
c = 0
e = 0
o = 1
for i in range(0, n):
	if i % 2 == 0:
		for j in range(0, n):
			print(e, end=' ')
			e = e + 2
		print()
	else:
		for j in range(0, n):
			print(o, end=' ')
			o = o + 2
		print()


n = int(input())
ans = [[0] * n for i in range(n)]
for i in range(n):
	for j in range(n):
		ans[i][j] = i % 4 * 4 + j % 4 + 16 * (i // 4) * (n // 4) + 16 * (j // 4)
for i in range(n):
	print(*ans[i])


n = int(input())
x = 0
y = 0
acum = 0
mt = [[-1 for i in range(n)] for j in range(n)]
for i in range(n // 4 * (n // 4)):
	for j in range(4):
		for k in range(4):
			mt[x + j][y + k] = acum
			acum += 1
	y += 4
	if y == n:
		y = 0
		x += 4
for i in mt:
	print(*i)


def findGrid(n):
	arr = [[0 for k in range(n)] for l in range(n)]
	x = 0
	for i in range(n // 4):
		for j in range(n // 4):
			for k in range(4):
				for l in range(4):
					arr[i * 4 + k][j * 4 + l] = x
					x += 1
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()
n = int(input())
findGrid(n)


import functools
import math
import sys
from collections import defaultdict
rt = lambda : map(int, input().split())
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))

def main():
	n = ri()
	pattern = [[0, 4, 8, 12], [13, 9, 5, 1], [2, 6, 10, 14], [15, 11, 7, 3]]
	a = [[0 for _ in range(n)] for _ in range(n)]
	blocks = n * n // 16
	posX = 0
	posY = 0
	for x in range(blocks):
		for i in range(4):
			for j in range(4):
				a[posX + i][posY + j] = pattern[i][j] + x * 16
		posX += 4
		if posX >= n:
			posX = 0
			posY += 4
	for row in a:
		print(' '.join(map(str, row)))
main()


n = int(input())
ans = [[0 for i in range(n)] for j in range(n)]
ans_for_4 = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
for i in range(n):
	for j in range(n):
		ans[i][j] = ans_for_4[i % 4][j % 4] + (i // 4 + n // 4 * (j // 4)) * 16
for i in range(n):
	print(' '.join([str(j) for j in ans[i]]))


def naiveSolve(n):
	return

def check(arr):
	xors = set()
	n = len(arr)
	for i in range(n):
		x = 0
		for j in range(n):
			x ^= arr[i][j]
		xors.add(x)
	for j in range(n):
		x = 0
		for i in range(n):
			x ^= arr[i][j]
		xors.add(x)
	print(xors)

def main():
	n = int(input())
	arr = makeArr(lambda : -1, [n, n])
	curr = 0
	for I in range(0, n, 4):
		for J in range(0, n, 4):
			for i in range(4):
				for j in range(4):
					arr[I + i][J + j] = curr
					curr += 1
	multiLineArrayOfArraysPrint(arr)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(i, j):
	print('? {} {}'.format(i, j))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(' '.join([str(x) for x in ans])))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


n = int(input())
c = 0
e = 0
o = 1
for i in range(0, n):
	if i % 2 == 0:
		for j in range(0, n):
			print(e, end=' ')
			e = e + 2
		print()
	else:
		for j in range(0, n):
			print(o, end=' ')
			o = o + 2
		print()


n = int(input())
u = []
for i in range(n):
	u.append([0] * n)
x = 0
for i in range(n // 2):
	for j in range(n // 2):
		u[i][j] = x * 4
		u[i + n // 2][j] = x * 4 + 1
		u[i + n // 2][j + n // 2] = x * 4 + 2
		u[i][j + n // 2] = x * 4 + 3
		x += 1
for i in range(n):
	print(' '.join(map(str, u[i])))


if True:
	for _ in range(1):
		n = int(input())
		array = [[0] * n for i in range(n)]
		arr = [[0] * n for i in range(n)]
		x = 0
		for i in range(n):
			for j in range(n):
				array[i][j] = x
				x += 1
		it = y = 0
		for i in range(n):
			for j in range(n // 2):
				arr[it][y] = array[j][i]
				it += 1
			if it == n:
				it = 0
				y += 1
		it = 0
		y = n // 2
		for i in range(n):
			for j in range(n // 2, n):
				arr[it][y] = array[j][i]
				it += 1
			if it == n:
				it = 0
				y += 1
		for i in range(n):
			print(*arr[i][:])


n = int(input())
i1 = 0
j1 = 1
ans = []
for i in range(n):
	for j in range(n):
		if i % 2 == 0:
			print(i1, end=' ')
			if j == 2:
				ans.append(i1)
			i1 += 2
		else:
			print(j1, end=' ')
			if j == 1:
				ans.append(j1)
			j1 += 2
	print()


n = int(input())
t = [[0] * n for i in range(n)]
last_even = 0
last_odd = 1
for j in range(n):
	for i in range(n):
		if j % 2 == 0:
			t[i][j] = last_even
			last_even += 2
		else:
			t[i][j] = last_odd
			last_odd += 2
for r in t:
	print(' '.join(map(str, r)))


n = int(input())
a = [[0] * n for i in range(n)]
p = 0
for i in range(n // 4):
	for j in range(n // 4):
		for k in range(i * 4, i * 4 + 4):
			for l in range(j * 4, j * 4 + 4):
				a[k][l] = p
				p += 1
for i in a:
	print(*i)


n = int(input())
g = n // 4
x = 0
for i in range(g):
	m = 0
	for j in range(4):
		for k in range(n):
			l = k // 4
			print(k % 4 + 16 * l + m + x, end=' ')
		m += 4
		print()
	x += n * 4


n = int(input())
c = 0
from copy import copy, deepcopy
answer = [[0 for i in range(n)] for i in range(n)]
for i in range(n // 2):
	a = list()
	for j in range(n):
		a.append(c)
		c += 1
	answer[i] = copy(a)
	answer[i + n // 2] = copy(a)
for i in range(n):
	for j in range(n):
		if i < n // 2:
			answer[i][j] = answer[i][j] * 2
		else:
			answer[i][j] = answer[i][j] * 2 + 1
print('\n'.join(map(lambda l: ' '.join(map(str, l)), answer)))


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def si():
	return input()

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = ii()
	l = [[0 for i in range(4)] for j in range(4)]
	c = 0
	f = 0
	for i in range(4):
		if f == 0:
			f = 1
			for j in range(4):
				l[i][j] = c
				c += 1
		else:
			f = 0
			for j in range(4 - 1, -1, -1):
				l[i][j] = c
				c += 1
	ans = [[0 for i in range(n)] for j in range(n)]
	i = 0
	j = 0
	prev = 0
	while i < n:
		p1 = 0
		for iii in range(i, i + 4):
			for jjj in range(j, j + 4):
				ans[iii][jjj] = l[iii - i][jjj - j] + prev
				p1 = max(ans[iii][jjj], p1)
		prev = p1 + 1
		j += 4
		if j == n:
			j = 0
			i += 4
	for i in ans:
		print(*i)
t = 1
for _ in range(t):
	solve()


n = int(input())
k = n // 4
seed = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
res = [[0] * n for i in range(n)]
for i in range(k):
	for j in range(k):
		tr = i * 4
		lc = j * 4
		d = i * k + j << 4
		for m in range(4):
			for l in range(4):
				r = tr + m
				c = lc + l
				res[r][c] = d + seed[m][l]
for i in range(n):
	print(' '.join((str(res[i][j]) for j in range(n))))


n = int(input())
x = 0
mat = [[-1 for i in range(n)] for i in range(n)]
for i in range(0, n, 4):
	for j in range(0, n, 4):
		for r in range(i, i + 4):
			for l in range(j, j + 4):
				mat[r][l] = x
				x += 1
for i in range(n):
	print(*mat[i])


def four(i, j):
	global a
	global s
	for ki in range(i, i + 4):
		for kj in range(j, j + 4):
			s += 1
			a[ki][kj] = s
n = int(input())
a = []
s = -1
for i in range(n):
	a.append([0] * n)
for i in range(len(a)):
	for j in range(len(a[i])):
		if a[i][j] == 0:
			four(i, j)
for i in range(len(a)):
	print(' '.join(list(map(str, a[i]))))


import sys

def main():
	n = int(sys.stdin.readline())
	ind = 0
	f = [[0] * n for i in range(n)]
	for i in range(0, n // 4):
		for j in range(0, n // 4):
			for p in range(4):
				for q in range(4):
					f[i * 4 + p][j * 4 + q] = ind
					ind += 1
	for i in range(n):
		for j in range(n):
			print(f[i][j], end=' ')
		print()
main()


import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = [[0 for i in range(n)] for j in range(n)]
c = 0
for tw in range(2):
	for k in range(2):
		t = c + 1
		c += 1
		t %= 4
		for i in range(n // 2):
			for j in range(n // 2):
				a[i + n // 2 * tw][j + n // 2 * k] = t
				t += 4
for i in range(n):
	print(*a[i], sep=' ')


def do():
	n = int(input())
	res = [[0] * n for _ in range(n)]
	cur = 0
	for i in range(0, n, 2):
		for j in range(0, n, 2):
			for ii in range(2):
				for jj in range(2):
					res[i + ii][j + jj] = cur
					cur += 1
	for row in res:
		print(' '.join(map(str, row)))
do()


import sys
import math
from collections import defaultdict, deque
n = int(sys.stdin.readline())
arr = [[0 for _ in range(n)] for x in range(n)]
count = 0
num = 0
for i in range(n // 2):
	for j in range(n // 2):
		arr[i][j] = num
		num += 1
		arr[i][j] = arr[i][j] * 4 + count
count = 1
num = 0
for i in range(n // 2, n):
	for j in range(n // 2):
		arr[i][j] = num
		num += 1
		arr[i][j] = arr[i][j] * 4 + count
count = 2
num = 0
for i in range(n // 2):
	for j in range(n // 2, n):
		arr[i][j] = num
		arr[i][j] = arr[i][j] * 4 + count
		num += 1
count = 3
num = 0
for i in range(n // 2, n):
	for j in range(n // 2, n):
		arr[i][j] = num
		arr[i][j] = arr[i][j] * 4 + count
		num += 1
for i in range(n):
	print(*arr[i])


n = int(input())
tx = 1
st = []
ls = []
for i in range(0, n):
	ot = tx
	l = []
	for j in range(0, n):
		l.append(tx - 1)
		tx += 1
		if j % 4 == 3:
			tx += 12
	if i % 4 != 3:
		tx = ot + 4
	else:
		tx -= 12
	ls.append(l)
	st.append(' '.join([str(j) for j in l]))
print('\n'.join(st))


N = int(input())
dp = []
for i in range(N):
	s = [0] * N
	dp.append(s)
t = 0
for i in range(0, N // 2):
	for j in range(0, N // 2):
		dp[i][j + N // 2] = 4 * t + 2
		dp[i][j] = 4 * t + 1
		dp[i + N // 2][j] = 4 * t + 3
		dp[i + N // 2][j + N // 2] = 4 * t
		t = t + 1
for i in range(0, N):
	for j in range(N):
		print(dp[i][j], end=' ')
	print(' ')


def main():
	n = int(input())
	for i in range(0, n * n, 4 * n):
		for j in range(i, i + 16, 4):
			line = []
			for k in range(j, j + n * 4, 16):
				line += [k, k + 1, k + 2, k + 3]
			print(*line)
main()


n = int(input())
x = 0
for q in range(0, n, 4):
	a = [[], [], [], []]
	for q1 in range(0, n, 4):
		for q2 in range(4):
			for q3 in range(4):
				a[q2].append(x + q2 * 4 + q3)
		x += 16
	for q1 in a:
		print(*q1)


n = int(input())
a = [[0] * n for _ in range(n)]

def solve(s, xo, yo, vo):
	if s <= 4:
		u = [list(map(int, '8 9 1 13'.split())), list(map(int, '3 12 7 5'.split())), list(map(int, '0 2 4 11'.split())), list(map(int, '6 10 15 14'.split()))]
		for i in range(4):
			for j in range(4):
				a[xo + i][yo + j] = u[i][j] + vo
		return
	bs = s // 4
	for i in range(bs):
		for j in range(bs):
			solve(4, xo + i * 4, yo + j * 4, 16 * (i * bs + j))
solve(n, 0, 0, 0)
for i in a:
	print(*i)


n = int(input())
a = [[-1] * n for i in range(n)]
q = 0
for i in range(0, n, 4):
	for j in range(0, n, 4):
		w = q
		for k in range(4):
			for l in range(4):
				a[i + k][j + l] = w
				w = w + 1
		q = q + 16
for i in range(len(a)):
	print(*a[i])


def check(a):
	n = len(a)
	for i in range(n):
		f1 = 0
		f2 = 0
		for j in range(n):
			f1 ^= a[i][j]
			f2 ^= a[j][i]
		if max(f1, f2) > 0:
			return False
	return True
n = int(input())
a = [[0 for i in range(n)] for j in range(n)]
d = 0
for i in range(n // 4):
	for j in range(n // 4):
		for x in range(i * 4, i * 4 + 4):
			for y in range(j * 4, j * 4 + 4):
				a[x][y] = d
				d += 1
for i in range(n):
	print(*a[i])


n = int(input())
a = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
b = [[0] * n for i in range(n)]
for i in range(n):
	for j in range(n):
		k = i // 4 * (n // 4) + j // 4
		b[i][j] = k * 16 + a[i % 4][j % 4]
res = []
for i in range(n):
	res.append(' '.join(map(str, b[i])))
print('\n'.join(res))


def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
n = ii()
for i in range(n):
	for j in range(n):
		print(4 * i + j % 4 + 4 * n * (j // 4), end=' ')
	print('')


n = int(input())
t = [[0] * n for _ in range(n)]
n2 = n // 2
c = 0
for i in range(n2):
	for j in range(n2):
		t[i][j] = c
		t[i + n2][j] = c + 1
		t[i][j + n2] = c + 2
		t[i + n2][j + n2] = c + 3
		c += 4
for r in t:
	print(' '.join((str(v) for v in r)))


n = int(input())
f = 0
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n // 2):
	for j in range(n // 2):
		a[i][j] = 4 * f + 1
		a[i][j + n // 2] = 4 * f + 2
		a[i + n // 2][j] = 4 * f + 3
		a[i + n // 2][j + n // 2] = 4 * f
		f += 1
for i in range(n):
	for j in range(n):
		print(a[i][j], end=' ')
	print('')


n = int(input())
grid = [[None for _ in range(n)] for _ in range(n)]
root = 2
fill = 0
for i in range(n // 2):
	for j in range(n // 2):
		grid[i][j] = 4 * fill + 1
		grid[i][j + n // 2] = 4 * fill + 2
		grid[i + n // 2][j] = 4 * fill + 3
		grid[i + n // 2][j + n // 2] = 4 * fill
		fill += 1
for x in grid:
	print(*x)


n = int(input())
a = [[0] * n for i in range(n)]
cnt_cnt = 0
cnt = n ** 2
for i in range(n):
	if cnt >= n ** 2:
		cnt = cnt_cnt
		cnt_cnt += 1
	for j in range(n):
		a[i][j] = cnt
		cnt += 4
for i in a:
	for j in i:
		print(j, end=' ')
	print()


from sys import stdout
print = lambda x: stdout.write(str(x) + ' ')
n = int(input())
a = [8, 9, 1, 13, 3, 12, 7, 5, 0, 2, 4, 11, 6, 10, 15, 14]
for i in range(n):
	for j in range(n):
		print(16 * (j // 4 + n // 4 * (i // 4)) ^ a[(j & 3) + 4 * (i & 3)])


n = int(input())
ans = []
for i in range(n):
	ans.append([0 for i in range(n)])
c = 0
for k in range(n // 4):
	for i in range(n):
		for j in range(4):
			ans[i][k * 4 + j] = c
			c += 1
for i in ans:
	print(*i)


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getArr():
	return list(map(int, input().split()))

def getStr():
	return input().strip()

def addDictList(d, key, val):
	if key not in d:
		d[key] = []
	d[key].append(val)

def addDictInt(d, key, val):
	if key not in d:
		d[key] = 0
	d[key] = val

def addDictCount(d, key):
	if key not in d:
		d[key] = 0
	d[key] += 1

def addDictSum(d, key, val):
	if key not in d:
		d[key] = 0
	d[key] += val
n = getInt()
a = []
for i in range(n):
	a.append([])
x = 0
for col in range(0, n, 4):
	for row in range(n):
		for col2 in range(4):
			a[col + col2].append(x)
			x += 1
for row in range(n):
	print(*a[row])


n = int(input())
k = n // 2
for i in range(k):
	row1 = []
	row2 = []
	for j in range(k):
		row1.append(str(4 * (i * k + j)))
		row1.append(str(4 * (i * k + j) + 1))
		row2.append(str(4 * (i * k + j) + 2))
		row2.append(str(4 * (i * k + j) + 3))
	print(' '.join(row1))
	print(' '.join(row2))


from collections import defaultdict as dc
import sys
import math

def inp():
	p = sys.stdin.readline()
	return p
n = int(inp())
p = [[0 for i in range(n)] for j in range(n)]
z = 0
for i in range(n // 4):
	for j in range(n // 4):
		for h in range(4):
			for k in range(4):
				p[i * 4 + h][j * 4 + k] = z
				z += 1
for i in range(n):
	print(*p[i])


n = int(input())
for i in range(n):
	for j in range(n):
		print(4 * i + j % 4 + 4 * n * (j // 4), end=' ')
	print('')


n = int(input())
for i in range(n):
	result = list()
	(m, k) = (i // 4, i % 4)
	for j in range(n):
		result.append(str(4 * n * m + 4 * j + k))
	print(' '.join(result))


n = int(input())
tile = [[8, 9, 1, 13], [3, 12, 7, 5], [0, 2, 4, 11], [6, 10, 15, 14]]
for r in range(n):
	line = ''
	for c in range(n):
		offset = r // 4 * (n // 4) + c // 4
		line += str(offset * 16 + tile[r % 4][c % 4]) + ' '
	print(line)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
from typing import List
N = int(input())
A = [[0 for _ in range(N)] for _ in range(N)]
v = 0
for i in range(N // 2):
	for j in range(N // 2):
		A[i][j] = 4 * v + 1
		A[i][j + N // 2] = 4 * v + 2
		A[i + N // 2][j] = 4 * v + 3
		A[i + N // 2][j + N // 2] = 4 * v
		v += 1
for row in A:
	print(' '.join(map(str, row)))


n = int(input())
ans = [[0] * n for _ in range(n)]
k = 0
for i in range(n // 2):
	for j in range(n // 2):
		ans[i][j] = 4 * k
		ans[i + n // 2][j] = 4 * k + 1
		ans[i][j + n // 2] = 4 * k + 2
		ans[i + n // 2][j + n // 2] = 4 * k + 3
		k += 1
for i in ans:
	print(*i)


n = int(input())
for j in range(n // 2):
	x = [4 * (k + j * n // 2) for k in range(n // 2)]
	x.extend([4 * (k + j * n // 2) + 1 for k in range(n // 2)])
	print(*x)
for j in range(n // 2):
	x = [4 * (k + j * n // 2) + 2 for k in range(n // 2)]
	x.extend([4 * (k + j * n // 2) + 3 for k in range(n // 2)])
	print(*x)