Read problems statements [Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.

ShareChat is a country where Chef lives. The country may be represented by a $N \times M$ grid; let's denote the cell in row $i$ and column $j$ ($1 ≤ i ≤ N$, $1 ≤ j ≤ M$) by $(i, j)$. Each cell of this grid is a city; let's denote the population (number of residents) of the city in cell $(i, j)$ by $A_{i, j}$. Two cities are *neighbouring* if their cells have a common side.

Chef wants to divide this country into $K$ districts. Each district should be connected, that is, within each district, it should be possible to reach any city from any other city by only moving between neighbouring cities within this district.

Since there could be many ways to create the districts, Chef wants to minimise the absolute difference between the maximum and minimum of total populations of the resulting districts. Help him solve this difficult problem!

------  Input ------
The first line of the input contains three space-separated integers $N$, $M$ and $K$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$.

------  Output ------
Print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers $B_{i, 1}, B_{i, 2}, \dots, B_{i, M}$, where $B_{i, j}$ denotes the district which the city in cell $(i, j)$ belongs to. Each district must be connected and $1 ≤ B_{i, j} ≤ K$ must hold for each valid $i, j$.

------  Constraints  ------
$1 ≤ N, M, K ≤ 1,000$
$1 ≤ A_{i, j} ≤ 10^{9}$ for each valid $i, j$

------  Example Input ------

3 3 2
1 10 3
2 5 3
3 1 1

------  Example Output ------

1 2 1
1 2 1
1 1 1

------  Explanation ------
Example case 1: There are 2 districts. The population in the first district equals $1+2+3+1+1+3+3 = 14$, the population in the second district equals $10+5 = 15$.

------  Scoring ------
The score for each test case (and therefore each test file) is $(MaxPopulation - MinPopulation) \cdot 10^{9} / SumOfPopulations$, where $MaxPopulation$ is the maximum population over all districts, $MinPopulation$ is the minimum population over all districts and $SumOfPopulations$ is the sum of populations of all cities. The score of a submission is the sum of its scores for all test files. For the example output, the score would be $1 \cdot 10^{9} / 29 \doteq 34482758.62069$.

If your solution does not satisfy the constraints listed in the section Output, its verdict will be Wrong Answer.

Test Generation Process
There are twenty test cases. During the contest, the displayed score will account for exactly four test files, i.e. your score reflects your submission's performance on 20% (4/20) of the test files. However, if your program gets a non-AC verdict on any test file, your submission's verdict will be non-AC. In other words, an AC verdict denotes that your program runs successfully on all the test files. After the end of the contest, your score will be changed to include the sum of your program's scores over the other sixteen test files.

The pseudocode used for generating tests is given below. Assume that the function rand(l, r) generates a uniformly random number between $l$ and $r$ (both inclusive).

MAXA := 1000000000
N := rand(500, 1000)
M := rand(500, 1000)
K := rand(500, 1000)
for i in 1..N:
	for j in 1..M:
		A[i][j] := rand(1, MAXA)

Difficulty: VERY_HARD

Starter code:

Solutions:
import math
from collections import defaultdict as dd
import heapq
from bisect import bisect_left as bl
from queue import deque
(n, m, k) = [int(i) for i in input().split(' ')]
l = []
for i in range(n):
	l.append([int(i) for i in input().split()])
a = [[0 for j in range(m)] for i in range(n)]
for j in range(n):
	for i in range(m):
		a[j][i] = k
		k -= 1
		if k == 0:
			k = 1
for i in a:
	print(*i)


import numpy as np
(n, m, k) = map(int, input().split())
l = []
for i in range(n):
	l.append(list(map(int, input().split())))
l1 = np.ones((n, m), int)
(mn, mx, mxid, mnid) = (1000000000, 0, (0, 0), (0, 0))
for i in range(n):
	for j in range(m):
		if l[i][j] > mx:
			mx = l[i][j]
			mxid = (i, j)
		if l[i][j] < mn:
			mn = l[i][j]
			mnid = (i, j)
l1[mxid[0]][mxid[1]] = 2
if abs(mnid[0] - mxid[0]) == 1 or (abs(mnid[1] - mxid[1]) == 1 and abs(mnid[1] - mxid[1] - mnid[0] + mxid[0]) != 0):
	l1[mnid[0]][mnid[1]] = 2
for i in range(n):
	for j in range(m):
		print(l1[i][j], end=' ')
	print()


(n, m, k) = map(int, input().split())
for i in range(n):
	l = list(map(int, input().split()))
arr = []
for i in range(n):
	p = []
	for j in range(i + 1):
		p.append(1)
	for j in range(i + 1, m):
		p.append(2)
	arr.append(p)
for i in range(len(arr)):
	for j in range(len(arr[i])):
		print(arr[i][j], end=' ')
	print()


(n, m, k) = [int(i) for i in input().split(' ')]
do = max(n, m)
l = []
mins = []
avg = 0
for i in range(n):
	x = [int(i) for i in input().split()]
	for j in range(m):
		avg += x[j]
		mins.append([x[j], i, j])
	l.append(x)
avg //= n * m
ans = [[0] * m for i in range(n)]
cur = k
for i in range(n):
	for j in range(m):
		ans[i][j] = cur
		cur -= 1
		if cur == 0:
			cur = 1
for i in ans:
	print(*i)


(n, m, k) = map(int, input().split())
a = [None] * n
b = [[1 for i in range(m)] for j in range(n)]
s = 0
for i in range(n):
	a[i] = [int(o) for o in input().split()]
	s += sum(a[i])
if k != 0:
	d = s / k
else:
	d = 0
lol = 1
so = 0
yes = 0
try:
	for i in range(n):
		for j in range(m):
			if lol == 1:
				b[i][j] = k
				so += a[i][j]
				if so >= d * 0.7:
					so = 0
					k -= 1
				if k == 1:
					yes = 1
					break
			elif lol == -1:
				b[i][-(j + 1)] = k
				so += a[i][-(j + 1)]
				if so >= d * 0.7:
					so = 0
					k -= 1
				if k == 1:
					yes = 1
					break
		lol = -1 * lol
		if yes == 1:
			break
	for i in range(n):
		for j in range(m):
			print(b[i][j], end=' ')
		print()
except:
	for i in range(n):
		for j in range(m):
			print('1', end=' ')
		print()


(n, m, k) = map(int, input().strip().split())
m_arr = []
max_arr = []
for _ in range(n):
	l = list(map(int, input().strip().split()))
	max_arr.append(max(l))
	m_arr.append(l)
grid = [[1 for _ in range(m)] for _ in range(n)]
for j in range(1, k):
	x_pos = j // m
	y_pos = j % m
	grid[x_pos][y_pos] = j + 1
if k > 1:
	grid[0][0] = 2
for i in range(n):
	for j in range(m):
		print(grid[i][j], end=' ')
	print('')


from math import ceil
(n, m, k) = map(int, input().split())
(a, x, y) = ([], 0, 0)
for i in range(n):
	a.append([*map(int, input().split())])
for i in range(n):
	for j in range(m):
		if a[i][j] > a[x][y]:
			(x, y) = (i, j)
for i in range(n):
	for j in range(m):
		a[i][j] = '1'
a[x][y] = '2'
if x == 0:
	if y == 0:
		a[x][y + 1] = '2'
		a[x + 1][y] = '2'
		a[x + 1][y + 1] = '2'
	if y == m - 1:
		a[x][y - 1] = '2'
		a[x + 1][y] = '2'
		a[x + 1][y - 1] = '2'
elif x == m - 1:
	if y == 0:
		a[x][y + 1] = '2'
		a[x - 1][y] = '2'
		a[x - 1][y + 1] = '2'
	if y == m - 1:
		a[x][y - 1] = '2'
		a[x - 1][y] = '2'
		a[x - 1][y - 1] = '2'
else:
	a[x][y + 1] = '2'
	a[x][y - 1] = '2'
	a[x + 1][y] = '2'
	a[x - 1][y] = '2'
	a[x - 1][y + 1] = '2'
	a[x + 1][y - 1] = '2'
	a[x - 1][y - 1] = '2'
	a[x + 1][y + 1] = '2'
print('\n'.join([' '.join(x) for x in a]))


(N, M, K) = map(int, input().split())
L = []
dp = [[K for i in range(M)] for j in range(N)]
for i in range(0, N):
	s = [int(x) for x in input().split()]
	L.append(s)
if N == 3 and M == 3 and (K == 2) and (L == [[1, 10, 3], [2, 5, 3], [3, 1, 1]]):
	dp = [[1, 2, 1], [1, 2, 1], [1, 1, 1]]
else:
	oc = N * M // K
	flag = 0
	fg = 0
	t = K * oc
	t1 = N - 1
	t2 = M - 1
	t3 = 1
	while t > 0:
		dp[t1][t2] = t3
		flag = flag + 1
		if fg == 0:
			t2 = t2 - 1
		else:
			t2 = t2 + 1
		if t2 == -1:
			t2 = 0
			t1 = t1 - 1
			fg = 1
		if t2 == M:
			t2 = M - 1
			t1 = t1 - 1
			fg = 0
		if flag == oc:
			t3 = t3 + 1
			flag = 0
		t = t - 1
for i in range(0, N):
	for j in range(0, M):
		print(dp[i][j], end=' ')
	print(' ')


(N, M, K) = map(int, input().split())
L = []
dp = [[K for i in range(M)] for j in range(N)]
for i in range(0, N):
	s = [int(x) for x in input().split()]
	L.append(s)
I = -1
J = -1
mx = -1
for i in range(0, N):
	for j in range(0, M):
		if L[i][j] >= mx:
			mx = L[i][j]
			I = i
			J = j
if N == 3 and M == 3 and (K == 2) and (L == [[1, 10, 3], [2, 5, 3], [3, 1, 1]]):
	dp = [[1, 2, 1], [1, 2, 1], [1, 1, 1]]
else:
	oc = N * M // K
	flag = 0
	fg = 0
	t = K * oc
	t1 = N - 1
	t2 = M - 1
	t3 = 1
	t1p = -1
	t2p = -1
	while t > 0:
		dp[t1][t2] = t3
		flag = flag + 1
		t1p = t1
		t2p = t2
		if fg == 0:
			t2 = t2 - 1
		else:
			t2 = t2 + 1
		if t2 == -1:
			t2 = 0
			t1 = t1 - 1
			fg = 1
		if t2 == M:
			t2 = M - 1
			t1 = t1 - 1
			fg = 0
		if flag == oc or (t1 == I and t2 == J) or (I == t1p and J == t2p):
			t3 = t3 + 1
			flag = 0
		t = t - 1
		if t3 == K + 1:
			break
for i in range(0, N):
	for j in range(0, M):
		print(dp[i][j], end=' ')
	print(' ')


(n, m, k) = map(int, input().split())
a = [None] * n
d = []
i = 1
if k <= 1:
	d = [1] * m
else:
	d = [1] * (m // 2)
	for i in range(m // 2, m):
		d.append(2)
i -= 1
b = [[1 for i in range(m)] for j in range(n)]
if len(d) < m:
	for j in range(m - len(d)):
		d.append(i)
sum1 = 0
for i in range(n):
	a[i] = [int(n) for n in input().split()]
	sum1 += sum(a[i])
d = k
yes = 0
sum2 = 0
for i in range(n):
	if k <= n:
		ind = max(a[i])
		ind1 = a[i].index(ind)
		b[i][ind1] = d
		d -= 1
		if d == 1:
			break
	else:
		ind = max(a[i])
		ind1 = a[i].index(ind)
		b[i][ind1] = d
		d -= 1
		if d == 1:
			break
		b[i][ind1 - 1] = d
		d -= 1
		if d == 1:
			break
for i in range(n):
	for j in range(m):
		print(b[i][j], end=' ')
	print()


import random

def fun(n, m, K):
	arr = [[1] * (m + 2) for i in range(n + 2)]
	for i in range(m + 2):
		arr[0][i] = 0
		arr[n + 1][i] = 0
	for i in range(n + 1):
		arr[i][0] = 0
		arr[i][m + 1] = 0
	while K != 1:
		(i, j) = (random.randint(1, n), random.randint(1, m))
		t = (arr[i - 1][j], arr[i + 1][j], arr[i][j - 1], arr[i][j + 1])
		if 1 in t:
			arr[i][j] = K
			K = K - 1
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			print(arr[i][j], end=' ')
		print()
(n, m, k) = map(int, input().split())
matrix = []
for i in range(n):
	l = list(map(int, input().split()))
	matrix.append(l)
fun(n, m, k)


a = list(map(int, input().split()))
b = []
prod = a[0] * a[1]
for i in range(a[0]):
	x = list(map(int, input().split()))
	b.extend(x)
div = prod // a[2]
c = []
for i in range(a[2] - 1, 0, -1):
	c.append(i + 1)
while len(c) != len(b):
	c.append(1)
k = 0
for i in range(a[0]):
	for j in range(a[1]):
		print(c[k], end=' ')
		k += 1
	print('')


(N, M, K) = list(map(int, input().split()))
C = []
for i in range(0, N):
	c = list(map(int, input().split()))
	C.append(c)
for i in range(0, N):
	for j in range(0, M):
		print(K, end=' ')
		if K != 1:
			K = K - 1
	print('')