#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 500
int map[MAXN][MAXN];
int parent[MAXN*MAXN];
int ans[MAXN][MAXN];
pair<int,int> coord[300001];
int R,C;
int find(int x){
if (parent[x] < 0){
return x;
}
return find(parent[x]);
}
bool is_range(int x, int y){
return (x >= 0 && y >= 0 && x <R && y <C);
}
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};
int max_num;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> R >> C;
memset(coord,-1,sizeof(coord));
memset(parent,-1,sizeof(parent));
for (int i = 0;i < R; i++){
for (int j = 0 ;j < C; j++){
cin >> map[i][j];
coord[map[i][j]] = {i,j};
max_num = max(max_num,map[i][j]);
}
}
for (int i = 0 ;i <= max_num; i++){
if (coord[i].first == -1) continue;
int sx = coord[i].first;
int sy = coord[i].second;
int num = i;
int x = -1;
int y = -1;
for (int k = 0;k< 8; k++){
int nx = sx + dx[k];
int ny = sy + dy[k];
if (!is_range(nx,ny)) continue;
if (map[nx][ny] < i){
if (num > map[nx][ny]){
num = map[nx][ny];
x = nx;
y = ny;
}
}
}
// 이동가능한 경우
if (x!=-1){
int a = sx*C+sy;
int b = x*C+y;
a = find(a);
b = find(b);
if (a != b) parent[a] = b;
}
}
for (int i = 0 ; i <R*C; i++){
int x,y;
if (parent[i] == -1){
x = i / C;
y = i % C;
}
else{
x = parent[i]/C;
y = parent[i]%C;
}
ans[x][y]++;
}
for (int i = 0;i <R; i++){
for (int j = 0 ;j < C; j++){
cout << ans[i][j] <<" ";
}
cout <<"\n";
}
return 0;
}