/************************************************
Grundlegende Algorithmen mit Java,
http://algorithmen-und-problemloesungen.de/
Copyright @2007-2008 by Doina Logofatu
************************************************/

import java.io.*;
import java.util.*;

public class P09PhotoProblem {
  private static final String FileInputName = "foto.in";
  private static final String FileOutputName = "foto.out";
  private int m, n;
  private int[][] a;

  P09PhotoProblem(int[][] a, int m, int n) {
    this.a = a;
    this.m = m;
    this.n = n;
  }

  private void color(int i, int j, int col) {
    if (0 <= i && i < m && 0 <= j && j < n && 1 == a[i][j]) {
      a[i][j] = col;
      color(i, j - 1, col);
      color(i, j + 1, col);
      color(i - 1, j, col);
      color(i + 1, j, col);
    }
  }

  void run(PrintStream out) {
    int col = 1;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++)
        if (1 == a[i][j])
          color(i, j, ++col);
    out.println(col);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        out.print(a[i][j]);
        out.print(' ');
      }
      out.println();
    }
  }

  public static void main(String[] args) throws IOException {
    Scanner sc = null;
    PrintStream out = null;
    try {
      out = new PrintStream(new File(FileOutputName));
      sc = new Scanner(new File(FileInputName));
      int m = sc.nextInt();
      int n = sc.nextInt();
      int[][] a = new int[m][n];

      for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
          a[i][j] = sc.nextInt();
        }
      }
      new P09PhotoProblem(a, m, n).run(out);
    } finally {
      if (sc != null) {
        sc.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
}

