#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int tomato[1000][1000];
int is_searched[1000][1000];
queue <int> x;
queue <int> y;
vector <int> other;
int di[4] = {1, -1, 0, 0};
int dj[4] = {0, 0, 1, -1};
int M, N;
int maximum = 0;
void bfs(int i, int j){
is_searched[i][j] = 1;
x.push(i);
y.push(j);
while(! x.empty()){
for(int a = 0; a < 4; a++){
int ni = x.front() + di[a];
int nj = y.front() + dj[a];
if(ni < 0 || ni >= N || nj < 0 || nj >=M){
continue;
}
if(tomato[ni][nj] == -1){
continue;
}
if(is_searched[ni][nj] == 0){
is_searched[ni][nj] = is_searched[x.front()][y.front()] + 1;
if(is_searched[ni][nj] > maximum){
maximum = is_searched[ni][nj];
}
x.push(ni);
y.push(nj);
}
}
x.pop();
y.pop();
}
}
int main()
{
cin >> M >> N;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
cin >> tomato[i][j];
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(tomato[i][j] == 1 && is_searched[i][j] == 0){
bfs(i, j);
}
}/*일단 숙성이 완료되었을 때의 날짜 구하기*/
}
cout << maximum << "\n";
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
cout << is_searched[i][j];
}
cout << "\n";
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(is_searched[i][j] == 0 && tomato[i][j] != -1){
cout << -1;
return 0;
}
}
}/*토마토가 모두 익지 못하는 상황*/
cout << 1;
int count = 0;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(tomato[i][j] == 1 || tomato[i][j] == -1){
count += 1;
}
}
}
if(count == N * M){
cout << 0;
return 0;
}/* 토마토가 첨부터 다 익음*/
cout << 2;
if(maximum == 0){
cout << 0;
}else{
cout << maximum - 1;
}
cout << 3;
return 0;/*토마토가 모두 익었을 때, 날짜를 구하기*/
}