package poj1088;
import java.util.Scanner;
public class Skining {
public static int[][] data;
public static int[][] visit;
public static int R;
public static int C;
public static int dirx[]={-1,0,1,0};
public static int diry[]={0,1,0,-1};
public static int maxlength;
public static boolean isEnd(int x,int y){
int lastx;
int lasty;
for(int i=0;i<4;i++){
lastx=x+dirx[i];
lasty=y+diry[i];
if(lastx>=0&&lastx<R&&lasty>=0&&lasty<C&&data[lastx][lasty]<data[x][y]) return false;
}
return true;
}
public static void dFS(int curx,int cury,int polength,int m,int n){
if(visit[curx][cury]!=0) {
if( maxlength<(visit[curx][cury]+polength-1))
maxlength=(visit[curx][cury]+polength-1) ;
visit[m][n]=maxlength;
return ;
}
if(isEnd(curx,cury)){
if(maxlength<polength) maxlength=polength;
visit[m][n]=maxlength;
return;
}
for(int i=0;i<4;i++){
int nextx=curx+dirx[i];
int nexty=cury+diry[i];
if(nextx>=0&&nextx<R&&nexty>=0&&nexty<C&&data[nextx][nexty]<data[curx][cury]){
dFS(nextx,nexty,polength+1,m,n);
}
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
R=sc.nextInt();
C=sc.nextInt();
data=new int[R][C];
visit=new int[R][C];
for(int i=0;i<R;i++){
for(int j=0;j<C;j++){
data[i][j]=sc.nextInt();
}
}
for(int i=0;i<R;i++){
for(int j=0;j<C;j++){
maxlength=0;
dFS(i,j,1,i,j);
}
}
System.out.println();
for(int i=0;i<R;i++){
for(int j=0;j<C;j++){
System.out.print(visit[i][j]+" ");
}
System.out.println();
}
System.out.print(maxlength);
}
}