You are given an empty grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). You should fill this grid with integers in a way that satisfies the following rules:
- For any three cells $c_1$, $c_2$ and $c_3$ such that $c_1$ shares a side with $c_2$ and another side with $c_3$, the integers written in cells $c_2$ and $c_3$ are distinct.
- Let's denote the number of different integers in the grid by $K$; then, each of these integers should lie between $1$ and $K$ inclusive.
- $K$ should be minimum possible.
Find the minimum value of $K$ and a resulting (filled) grid. If there are multiple solutions, you may find any one.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $M$.

-----Output-----
- For each test case, print $N+1$ lines.
- The first line should contain a single integer — the minimum $K$.
- Each of the following $N$ lines should contain $M$ space-separated integers between $1$ and $K$ inclusive. For each valid $i, j$, the $j$-th integer on the $i$-th line should denote the number in the $i$-th row and $j$-th column of the grid.

-----Constraints-----
- $1 \le T \le 500$
- $1 \le N, M \le 50$
- the sum of $N \cdot M$ over all test cases does not exceed $7 \cdot 10^5$

-----Subtasks-----
Subtask #1 (100 points): original constraints

-----Example Input-----
2
1 1
2 3

-----Example Output-----
1
1
3
1 1 2
2 3 3

-----Explanation-----
Example case 1: There is only one cell in the grid, so the only valid way to fill it is to write $1$ in this cell. Note that we cannot use any other integer than $1$.
Example case 2: For example, the integers written in the neighbours of cell $(2, 2)$ are $1$, $2$ and $3$; all these numbers are pairwise distinct and the integer written inside the cell $(2, 2)$ does not matter. Note that there are pairs of neighbouring cells with the same integer written in them, but this is OK.

Difficulty: MEDIUM_HARD

Solutions:
T = int(input())
for test in range(0, T):
	(n, m) = input().split(' ')
	n = int(n)
	m = int(m)
	tem = 0
	if n != 1 and m != 1:
		t1 = n
		t2 = m
		n = n + n % 2
		m = m + m % 2
		c1 = 0
		grid1 = [[]]
		c2 = 0
		grid2 = [[]]
		for i in range(0, m):
			temp = (i + 2) // 2
			if temp % 2 == 1:
				grid1[0].append(1)
			else:
				grid1[0].append(2)
		if m > 2:
			c1 = 2
		else:
			c1 = 1
		for i in range(1, n):
			grid1.append([])
			for j in range(0, m):
				l = []
				if i - 1 >= 0 and j - 1 >= 0:
					l.append(grid1[i - 1][j - 1])
				if i - 1 >= 0 and j + 1 < m:
					l.append(grid1[i - 1][j + 1])
				if j - 2 >= 0:
					l.append(grid1[i][j - 2])
				if i - 2 >= 0:
					l.append(grid1[i - 2][j])
				f = 0
				for x in range(1, c1 + 1):
					l.append(x)
					if l.index(x) == len(l) - 1:
						f = 1
						break
				if f == 1:
					grid1[i].append(x)
				else:
					c1 = c1 + 1
					grid1[i].append(c1)
		for i in range(0, n):
			temp = (i + 2) // 2
			if temp % 2 == 1:
				grid2[0].append(1)
			else:
				grid2[0].append(2)
		if n > 2:
			c2 = 2
		else:
			c2 = 1
		for i in range(1, m):
			grid2.append([])
			for j in range(0, n):
				l = []
				if i - 1 >= 0 and j - 1 >= 0:
					l.append(grid2[i - 1][j - 1])
				if i - 1 >= 0 and j + 1 < n:
					l.append(grid2[i - 1][j + 1])
				if j - 2 >= 0:
					l.append(grid2[i][j - 2])
				if i - 2 >= 0:
					l.append(grid2[i - 2][j])
				f = 0
				for x in range(1, c2 + 1):
					l.append(x)
					if l.index(x) == len(l) - 1:
						f = 1
						break
				if f == 1:
					grid2[i].append(x)
				else:
					c2 = c2 + 1
					grid2[i].append(c2)
		n = t1
		m = t2
		if c1 <= c2:
			print(c1)
			for i in range(0, n):
				for j in range(0, m - 1):
					print(grid1[i][j], end=' ')
				print(grid1[i][m - 1])
		else:
			print(c2)
			for i in range(0, n):
				for j in range(0, m - 1):
					print(grid2[j][i], end=' ')
				print(grid2[m - 1][i])
	elif n == 1:
		if m > 2:
			print(2)
		else:
			print(1)
		for i in range(0, m - 1):
			temp = (i + 2) // 2
			if temp % 2 == 1:
				print(1, end=' ')
			else:
				print(2, end=' ')
		if (m + 1) // 2 % 2 == 1:
			print(1)
		else:
			print(2)
	else:
		if n > 2:
			print(2)
		else:
			print(1)
		for i in range(0, n):
			temp = (i + 2) // 2
			if temp % 2 == 1:
				print(1)
			else:
				print(2)


def normalGrid(n, m):
	(res, temp, temp1, temp2, temp3) = ([], [], [], [], [])
	for i in range(m):
		if i % 4 <= 1:
			temp.append(1)
			temp1.append(3)
			temp2.append(2)
			temp3.append(4)
		else:
			temp.append(2)
			temp1.append(4)
			temp2.append(1)
			temp3.append(3)
	for i in range(n):
		if i % 4 == 0:
			res.append(temp)
		elif i % 4 == 1:
			res.append(temp1)
		elif i % 4 == 2:
			res.append(temp2)
		else:
			res.append(temp3)
	for i in res:
		print(*i)

def grid23(n, m):
	(temp, temp1) = ([], [])
	for i in range(m):
		if i % 6 == 0 or i % 6 == 1:
			temp.append(1)
		elif i % 6 == 2 or i % 6 == 3:
			temp.append(2)
		else:
			temp.append(3)
	for i in range(m):
		if i % 6 == 0 or i % 6 == 5:
			temp1.append(2)
		elif i % 6 == 1 or i % 6 == 2:
			temp1.append(3)
		else:
			temp1.append(1)
	print(*temp)
	print(*temp1)

def grid32(n, m):
	(t1, t2, t3, t4, t5, t6) = ([1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 2])
	for i in range(n):
		if i % 6 == 0:
			print(*t1)
		elif i % 6 == 1:
			print(*t2)
		elif i % 6 == 2:
			print(*t3)
		elif i % 6 == 3:
			print(*t4)
		elif i % 6 == 4:
			print(*t5)
		else:
			print(*t6)
for _ in range(int(input())):
	(n, m) = [int(i) for i in input().split()]
	if n >= 3 and m >= 3:
		print('4')
		normalGrid(n, m)
	elif n == 1:
		if m == 1:
			print('1\n1')
		elif m == 2:
			print('1\n1 1')
		elif m >= 3:
			print('2')
			res = []
			for i in range(m):
				if i % 4 == 0 or i % 4 == 1:
					res.append(1)
				else:
					res.append(2)
			print(*res)
	elif n == 2:
		if m == 1:
			print(1)
			print('1\n1')
		elif m == 2:
			print(2)
			print('1 1\n2 2')
		elif m >= 3:
			print('3')
			grid23(n, m)
	elif n >= 3:
		if m == 1:
			print('2')
			(res, temp, temp1) = ([], [1], [2])
			for i in range(n):
				if i % 4 == 1 or i % 4 == 0:
					res.append(temp)
				else:
					res.append(temp1)
			for i in res:
				print(*i)
		elif m == 2:
			print('3')
			grid32(n, m)


t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().strip().split(' '))
	if min(n, m) >= 3:
		p1 = [0] * m
		p2 = [0] * m
		p3 = [0] * m
		p4 = [0] * m
		for i in range(0, m, 2):
			if i % 4 == 2:
				p1[i] = 2
				p2[i] = 1
				p3[i] = 4
				p4[i] = 3
				if i + 1 < m:
					p1[i + 1] = 2
					p2[i + 1] = 1
					p3[i + 1] = 4
					p4[i + 1] = 3
			elif i % 4 == 0:
				p1[i] = 1
				p2[i] = 2
				p3[i] = 3
				p4[i] = 4
				if i + 1 < m:
					p1[i + 1] = 1
					p2[i + 1] = 2
					p3[i + 1] = 3
					p4[i + 1] = 4
		print(4)
		for i in range(n):
			if i % 4 == 0:
				print(*p1)
			elif i % 4 == 1:
				print(*p3)
			elif i % 4 == 2:
				print(*p2)
			else:
				print(*p4)
	elif m == n and m == 2:
		print(2)
		p1 = [1, 1]
		p2 = [2, 2]
		print(*p1)
		print(*p2)
	elif m != n and min(m, n) == 2:
		p1 = [0] * max(m, n)
		p2 = [0] * max(m, n)
		for i in range(0, max(m, n), 2):
			if i % 6 == 4:
				p1[i] = 3
				p2[i] = 2
				if i + 1 < len(p1):
					p1[i + 1] = 3
					p2[i + 1] = 2
			elif i % 6 == 2:
				p1[i] = 2
				p2[i] = 1
				if i + 1 < len(p1):
					p1[i + 1] = 2
					p2[i + 1] = 1
			elif i % 6 == 0:
				p1[i] = 1
				p2[i] = 3
				if i + 1 < len(p1):
					p1[i + 1] = 1
					p2[i + 1] = 3
		del p2[-1]
		p2 = [2] + p2
		print(3)
		if n == 2:
			print(*p1)
			print(*p2)
		else:
			for i in range(max(m, n)):
				print(p1[i], p2[i])
	elif n == 1 or m == 1:
		p1 = [0] * max(m, n)
		for i in range(0, max(m, n), 2):
			if i % 4 == 2:
				p1[i] = 2
				if i + 1 < max(m, n):
					p1[i + 1] = 2
			elif i % 4 == 0:
				p1[i] = 1
				if i + 1 < max(m, n):
					p1[i + 1] = 1
		if 2 in p1:
			print(2)
		else:
			print(1)
		if n == 1:
			print(*p1)
		else:
			for c in p1:
				print(c)


t = int(input())
for _ in range(t):
	(r, c) = map(int, input().split())
	c1 = c
	if c % 2 == 1:
		c1 = c + 1
		mat = [[0] * (c + 1) for _ in range(r)]
	else:
		mat = [[0] * c for _ in range(r)]
	k1 = 1
	for i in range(r):
		for j in range(c1):
			arr = [0] * 6
			if i - 2 >= 0:
				v = mat[i - 2][j]
				arr[v] = 1
			if j - 2 >= 0:
				v = mat[i][j - 2]
				arr[v] = 1
			if i - 1 >= 0 and j - 1 >= 0:
				v = mat[i - 1][j - 1]
				arr[v] = 1
			if i - 1 >= 0 and j + 1 < c1:
				v = mat[i - 1][j + 1]
				arr[v] = 1
			for k in range(1, 6):
				if arr[k] == 0:
					mat[i][j] = k
					if k1 < k:
						k1 = k
					break
	(n, m) = (c, r)
	m1 = m
	if m % 2 == 1:
		m1 = m + 1
		mat1 = [[0] * (m + 1) for _ in range(n)]
	else:
		mat1 = [[0] * m for _ in range(n)]
	k2 = 1
	for i in range(n):
		for j in range(m1):
			arr = [0] * 6
			if i - 2 >= 0:
				v = mat1[i - 2][j]
				arr[v] = 1
			if j - 2 >= 0:
				v = mat1[i][j - 2]
				arr[v] = 1
			if i - 1 >= 0 and j - 1 >= 0:
				v = mat1[i - 1][j - 1]
				arr[v] = 1
			if i - 1 >= 0 and j + 1 < m1:
				v = mat1[i - 1][j + 1]
				arr[v] = 1
			for k in range(1, 6):
				if arr[k] == 0:
					mat1[i][j] = k
					if k2 < k:
						k2 = k
					break
	if k1 <= k2:
		print(k1)
		for l in mat:
			l = l[:c]
			print(' '.join(map(str, l)))
	else:
		if m1 > m:
			for (i, l) in enumerate(mat1):
				mat1[i] = l[:m]
		mat2 = [[mat1[j][i] for j in range(n)] for i in range(m)]
		print(k2)
		for l in mat2:
			print(' '.join(map(str, l)))


a = []
for i in range(20):
	b = [1, 1, 2, 2]
	b = b * 20
	a.append(b[:50])
	b = [3, 3, 4, 4]
	b = b * 20
	a.append(b[:50])
	b = [2, 2, 1, 1]
	b = b * 20
	a.append(b[:50])
	b = [4, 4, 3, 3]
	b = b * 20
	a.append(b[:50])
a = a[:50]
for _ in range(int(input())):
	[n, m] = [int(i) for i in input().split()]
	if n == 2 and m == 2:
		print(2)
		print(1, 1)
		print(2, 2)
		continue
	if n == 2 and m > 2:
		x = []
		b = [1, 1, 2, 2, 3, 3]
		b *= 20
		b = b[:m]
		x.append(b)
		b = [3, 3, 1, 1, 2, 2]
		b *= 20
		b = [2] + b
		b = b[:m]
		x.append(b)
		print(3)
		print(*x[0])
		print(*x[1])
		continue
	if m == 2 and n > 2:
		x = []
		b = [1, 1, 2, 2, 3, 3]
		b *= 20
		b = b[:n]
		x.append(b)
		b = [3, 3, 1, 1, 2, 2]
		b *= 20
		b = [2] + b
		b = b[:n]
		x.append(b)
		print(3)
		x = zip(*x)
		for i in x:
			print(*i)
		continue
	x = [i[:m] for i in a[:n]]
	y = [i[:n] for i in a[:m]]
	y = list(zip(*y))
	mx = max((max((j for j in i)) for i in x))
	my = max((max((j for j in i)) for i in y))
	if my < mx:
		print(my)
		for i in y:
			print(*i)
	else:
		print(mx)
		for i in x:
			print(*i)