# Table Compression

Little Petya is now fond of data compression algorithms. He has already studied gzbzzip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.

Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a‘ consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table a i, j < a i, k, then in the resulting table a i, j < a i, k, and if a i, j = a i, k then a i, j = a i, k. Similarly, if in some column j of the initial table a i, j < a p, j then in compressed table a i, j < a p, j and if a i, j = a p, j then a i, j = a p, j.

Because large values require more space to store them, the maximum value in a‘ should be as small as possible.

Petya is good in theory, however, he needs your help to implement the algorithm.

Input

The first line of the input contains two integers n and m ( , the number of rows and the number of columns of the table respectively.

Each of the following n rows contain m integers a i, j (1 ≤ a i, j ≤ 109) that are the values in the table.

Output

Output the compressed table in form of n lines each containing m integers.

If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.

Examples

input

2 21 23 4

output

1 22 3

input

4 320 10 3050 40 3050 60 7090 80 70

output

2 1 35 4 35 6 79 8 7

Note

In the first sample test, despite the fact a 1, 2 ≠ a 21, they are not located in the same row or column so they may become equal after the compression.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 1000010;

vector <int> eq[N], mr[N];
pair <int, int> b[N], c[N];
int a[N];
bool was[N];
int x[N];
int res[N];

int main() {
int h, w;
scanf("%d %d", &h, &w);
for (int i = 0; i < h * w; i++) {
scanf("%d", a + i);
eq[i].clear();
mr[i].clear();
}
for (int row = 0; row < h; row++) {
for (int j = 0; j < w; j++) {
int id = row * w + j;
b[j] = make_pair(a[id], id);
}
sort(b, b + w);
for (int j = 1; j < w; j++) {
int x = b[j].second;
int y = b[j - 1].second;
if (b[j].first == b[j - 1].first) {
eq[x].push_back(y);
eq[y].push_back(x);
} else {
mr[x].push_back(y);
}
}
}
for (int col = 0; col < w; col++) {
for (int i = 0; i < h; i++) {
int id = i * w + col;
b[i] = make_pair(a[id], id);
}
sort(b, b + h);
for (int j = 1; j < h; j++) {
int x = b[j].second;
int y = b[j - 1].second;
if (b[j].first == b[j - 1].first) {
eq[x].push_back(y);
eq[y].push_back(x);
} else {
mr[x].push_back(y);
}
}
}
for (int i = 0; i < h * w; i++) {
was[i] = false;
c[i] = make_pair(a[i], i);
}
sort(c, c + h * w);
for (int id = 0; id < h * w; id++) {
int q = c[id].second;
if (was[q]) {
continue;
}
int b = 0, e = 0;
x = q;
was[q] = true;
while (b <= e) {
int sz = eq[x[b]].size();
for (int j = 0; j < sz; j++) {
int u = eq[x[b]][j];
if (!was[u]) {
e++;
x[e] = u;
was[u] = true;
}
}
b++;
}
e++;
int put = 1;
for (int jj = 0; jj < e; jj++) {
int u = x[jj];
int sz = mr[u].size();
for (int j = 0; j < sz; j++) {
int v = mr[u][j];
put = max(put, res[v] + 1);
}
}
for (int jj = 0; jj < e; jj++) {
int u = x[jj];
res[u] = put;
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (j > 0) {
putchar(' ');
}
printf("%d", res[i * w + j]);
}
printf("\n");
}
return 0;
}