// File: /workspace/github_cpp_makefile_star2_O2/razimantv[P]DancingLinks/rectangles/rectangles.cpp
// Function: main
int main() {
  // Read the dimensions of the board
  int H, W;
  std::cin >> H >> W;
  if (H < 1 || W<1) {
    std::cerr << "Invalid board size" << std::endl;
    return 0;
  }
  std::vector<std::vector<int>> board(H), prefixsum(H);

  // Read the board and compute prefix sums
  for (int i = 0; i < H; ++i) {
    board[i].resize(W);
    prefixsum[i].resize(W);
    for (int j = 0; j < W; ++j) {
      std::cin >> board[i][j];
      if (board[i][j] < 0) {
        std::cerr << "Invalid board size" << std::endl;
        return 0;
      }
      prefixsum[i][j] = elem(prefixsum, i, j - 1) + elem(prefixsum, i - 1, j) -
                        elem(prefixsum, i - 1, j - 1) + (board[i][j] > 0);
    }
  }

  // Generate the possible rectangles which contain the cell and have right area
  // These will be the subsets which we try to cover the set elements with
  std::vector<std::tuple<int, int, int, int>> rectangles; // i1, i2, j1, j2
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (board[i][j] == 0) continue;
      // Loop over all possibilities of rectangle size
      for (int h = board[i][j] / (W + 1) + 1; h <= H; ++h) {
        if (board[i][j] % h) continue;
        int w = board[i][j] / h;

        // Loop over all possibilities of rectangle position
        for (int i1 = std::max(0, i - h + 1), i2 = i1 + h - 1;
             i1 <= i && i2 < H; ++i1, ++i2) {
          for (int j1 = std::max(0, j - w + 1), j2 = j1 + w - 1;
               j1 <= j && j2 < W; ++j1, ++j2) {
            // There should be only one non-zero cell in the rectangle
            int cnt = elem(prefixsum, i2, j2) - elem(prefixsum, i1 - 1, j2) -
                      elem(prefixsum, i2, j1 - 1) +
                      elem(prefixsum, i1 - 1, j1 - 1);
            if (cnt != 1) continue;
            rectangles.push_back(std::make_tuple(i1, i2, j1, j2));
          }
        }
      }
    }
  }

  // Rectangles are the rows and cells are the columns
  // Let us build the adjacency matrix now
  int R = rectangles.size(), C = H * W;
  char* mat;
  mat = new (std::nothrow) char[R * C];
  if (mat == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  memset(mat, 0, R * C);
  for (int r = 0; r < rectangles.size(); ++r) {
    auto rect = rectangles[r];
    for (int i = std::get<0>(rect); i <= std::get<1>(rect); ++i) {
      for (int j = std::get<2>(rect); j <= std::get<3>(rect); ++j) {
        int c = i * W + j;
        mat[r * C + c] = 1;
      }
    }
  }

  // Solve using dancing links
  DancingLinks N;
  N.Create(R, C, 0, mat);
  std::vector<std::vector<int> > all;
  std::vector<int> cur;
  N.solve(all, cur, false);

  if(all.size() != 1) {
    std::cerr << "No solution found" << std::endl;
  }

  // Assign board vales as unique rectangle IDs
  for (int r = 0; r < all[0].size(); ++r) {
    auto rect = rectangles[all[0][r]];
    for (int i = std::get<0>(rect); i <= std::get<1>(rect); ++i) {
      for (int j = std::get<2>(rect); j <= std::get<3>(rect); ++j) {
        board[i][j] = r;
      }
    }
  }

  // And print the board
  for (const auto& row : board) {
    std::copy(row.begin(), row.end(),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq 0x4ce4(%rip), %rdi      # 0x5fd8
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x10a0
leaq 0x4(%rsp), %rsi
movq %rax, %rdi
callq 0x10a0
movslq (%rbx), %rsi
testq %rsi, %rsi
jle 0x1423
cmpl $0x0, 0x4(%rsp)
jle 0x1423
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x194a
movslq 0x8(%rsp), %rsi
leaq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x194a
xorl %r14d, %r14d
movslq 0x8(%rsp), %r8
cmpq %r8, %r14
jge 0x1487
imulq $0x18, %r14, %r15
movq 0x10(%rsp), %rdi
addq %r15, %rdi
movslq 0x4(%rsp), %rsi
callq 0x199c
movq 0x40(%rsp), %rdi
addq %r15, %rdi
movslq 0x4(%rsp), %rsi
callq 0x199c
leaq -0x1(%r14), %r12
imulq $0x18, %r12, %r13
xorl %ebp, %ebp
xorl %ebx, %ebx
movslq 0x4(%rsp), %rax
cmpq %rax, %rbx
jge 0x141b
movq 0x10(%rsp), %rax
movq (%rax,%r15), %rsi
addq %rbp, %rsi
movq 0x4c2a(%rip), %rdi      # 0x5fd8
callq 0x10a0
movq 0x10(%rsp), %rax
movq (%rax,%r15), %rax
movl (%rax,%rbp), %eax
testl %eax, %eax
js 0x1442
xorl %esi, %esi
movq 0x40(%rsp), %rcx
movl $0x0, %edx
testq %rbx, %rbx
je 0x13dc
movq (%rcx,%r15), %rdx
movl -0x4(%rdx,%rbp), %edx
testq %r14, %r14
je 0x13e8
movq (%rcx,%r13), %rsi
movl (%rsi,%rbp), %esi
decq %rbx
xorl %edi, %edi
movl %ebx, %r8d
orl %r12d, %r8d
js 0x13fd
movq (%rcx,%r13), %rdi
movl -0x4(%rdi,%rbp), %edi
cmpl $0x1, %eax
sbbl $-0x1, %edx
addl %esi, %edx
subl %edi, %edx
movq (%rcx,%r15), %rax
movl %edx, (%rax,%rbp)
addq $0x4, %rbp
addq $0x2, %rbx
jmp 0x138d
incq %r14
jmp 0x134b
movq 0x4bc6(%rip), %rdi      # 0x5ff0
leaq 0x1bd3(%rip), %rsi      # 0x3004
callq 0x10e0
movq %rax, %rdi
callq 0x1070
xorl %ebx, %ebx
jmp 0x1473
movq 0x4ba7(%rip), %rdi      # 0x5ff0
leaq 0x1bb4(%rip), %rsi      # 0x3004
callq 0x10e0
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x1070
leaq 0x40(%rsp), %rdi
callq 0x19ec
leaq 0x10(%rsp), %rdi
callq 0x19ec
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %edi, %edi
movslq %r8d, %rax
cmpq %rax, %rdi
jge 0x16a0
movl 0x4(%rsp), %ecx
imulq $0x18, %rdi, %r9
xorl %r13d, %r13d
movq %rdi, 0x98(%rsp)
movq %r9, 0x58(%rsp)
movslq %ecx, %rax
cmpq %rax, %r13
jge 0x1698
movq 0x10(%rsp), %rax
movq (%rax,%r9), %rax
movl (%rax,%r13,4), %eax
testl %eax, %eax
je 0x1690
leal 0x1(%rcx), %esi
cltd
idivl %esi
movl %eax, %r10d
movq 0x10(%rsp), %rsi
addq %r9, %rsi
movl %edi, %r12d
subl %r10d, %r12d
cmpl %r8d, %r10d
jge 0x1690
incl %r10d
movq (%rsi), %rax
movl (%rax,%r13,4), %eax
cltd
idivl %r10d
decl %r12d
testl %edx, %edx
jne 0x14f5
movl %edi, %r11d
subl %r10d, %r11d
pushq $-0x1
popq %rdx
cmovsl %edx, %r11d
incl %r11d
testl %r12d, %r12d
cmovsl %edx, %r12d
movl %r10d, 0xc(%rsp)
addl %r10d, %r12d
movl %r13d, %edx
subl %eax, %edx
movslq %edx, %rsi
testq %rsi, %rsi
pushq $-0x1
popq %rdx
cmovsq %rdx, %rsi
movq %rsi, 0xa0(%rsp)
movl %eax, %ebx
movl %r11d, %eax
cmpq %rax, %rdi
jb 0x1681
cmpl %r8d, %r12d
jge 0x1681
movl %r12d, %r14d
leal -0x1(%r11), %ebp
movq 0xa0(%rsp), %r15
movq %rbp, 0xa8(%rsp)
cmpq %r15, %r13
jle 0x1669
leaq (%rbx,%r15), %rax
cmpl %ecx, %eax
jge 0x1669
xorl %esi, %esi
movl %eax, %edx
orl %r12d, %edx
movl $0x0, %edx
js 0x15b1
movq 0x40(%rsp), %rdx
imulq $0x18, %r14, %rdi
movl %eax, %r8d
movq (%rdx,%rdi), %rdx
movl (%rdx,%r8,4), %edx
movl %eax, %edi
orl %ebp, %edi
js 0x15cb
movq 0x40(%rsp), %rsi
imulq $0x18, %rbp, %rdi
movl %eax, %r8d
movq (%rsi,%rdi), %rsi
movl (%rsi,%r8,4), %esi
xorl %edi, %edi
movl %r15d, %r8d
orl %r12d, %r8d
movl $0x0, %r8d
js 0x15ef
movq 0x40(%rsp), %r8
imulq $0x18, %r14, %r9
movl %r15d, %r10d
movq (%r8,%r9), %r8
movl (%r8,%r10,4), %r8d
movl %r15d, %r9d
orl %ebp, %r9d
js 0x160b
movq 0x40(%rsp), %rdi
imulq $0x18, %rbp, %r9
movl %r15d, %r10d
movq (%rdi,%r9), %rdi
movl (%rdi,%r10,4), %edi
incq %r15
addl %r8d, %esi
subl %esi, %edx
addl %edi, %edx
cmpl $0x1, %edx
jne 0x157a
movq %r14, %rbp
movq %rbx, %r14
movq %r13, %rbx
movl %eax, 0x28(%rsp)
movl %r15d, 0x2c(%rsp)
movl %r12d, 0x30(%rsp)
movq %r11, %r13
movl %r11d, 0x34(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1d3c
movl 0x4(%rsp), %ecx
movq %r13, %r11
movq %rbx, %r13
movq %r14, %rbx
movq %rbp, %r14
movq 0xa8(%rsp), %rbp
jmp 0x157a
incl %r11d
incl %r12d
movl 0x8(%rsp), %r8d
movq 0x98(%rsp), %rdi
jmp 0x154e
movq 0x58(%rsp), %r9
movl 0xc(%rsp), %r10d
jmp 0x14e7
incq %r13
jmp 0x14bd
incq %rdi
jmp 0x1499
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rbx
subq %r15, %rbx
sarq $0x4, %rbx
movl 0x4(%rsp), %ebp
imull %ebp, %r8d
movq %r8, %r12
movl %r8d, %eax
imull %ebx, %eax
movslq %eax, %r13
movq 0x48e4(%rip), %rsi      # 0x5fb0
movq %r13, %rdi
callq 0x1170
testq %rax, %rax
je 0x18c1
movq %rax, %r14
movq %r13, %rdx
xorl %r13d, %r13d
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
xorl %eax, %eax
cmpq %rbx, %rax
je 0x174c
movq %rax, %rdi
shlq $0x4, %rdi
movslq (%r15,%rdi), %rcx
movslq 0x4(%r15,%rdi), %rdx
movl 0x8(%r15,%rdi), %esi
movl 0xc(%r15,%rdi), %edi
movl %ebp, %r8d
imull %edi, %r8d
addl %edx, %r8d
addl %r13d, %r8d
movl %r8d, %r9d
movq %rdx, %r10
cmpl %esi, %edi
jg 0x1744
cmpq %rcx, %r10
jg 0x173d
movslq %r9d, %r9
movb $0x1, (%r14,%r9)
incq %r10
incl %r9d
jmp 0x1728
incl %edi
addl %ebp, %r8d
jmp 0x171e
incq %rax
addl %r12d, %r13d
jmp 0x16f2
leaq 0x28(%rsp), %rdi
callq 0x1fe4
leaq 0x28(%rsp), %rdi
movl %ebx, %esi
movl %r12d, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x20ce
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0xc0(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $-0x1
popq %r8
xorl %ecx, %ecx
callq 0x23e0
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %rax
subq %rcx, %rax
cmpq $0x18, %rax
je 0x17d9
movq 0x4833(%rip), %rdi      # 0x5ff0
leaq 0x1853(%rip), %rsi      # 0x3017
callq 0x10e0
movq %rax, %rdi
callq 0x1070
movq 0x80(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x60(%rsp), %rdx
xorl %esi, %esi
cmpq %rcx, %rsi
je 0x183a
movslq (%rax,%rsi,4), %r10
shlq $0x4, %r10
movslq (%rdx,%r10), %rdi
movslq 0x4(%rdx,%r10), %r8
movslq 0x8(%rdx,%r10), %r9
movslq 0xc(%rdx,%r10), %r10
cmpq %r9, %r10
jg 0x1835
imulq $0x18, %r10, %r11
addq 0x10(%rsp), %r11
movq %r8, %rbx
cmpq %rdi, %rbx
jg 0x1830
movq (%r11), %r14
movl %esi, (%r14,%rbx,4)
incq %rbx
jmp 0x181f
incq %r10
jmp 0x180e
incq %rsi
jmp 0x17ee
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
movq 0x477d(%rip), %rbx      # 0x5fc8
leaq 0x17d7(%rip), %rbp      # 0x3029
leaq 0xd8(%rsp), %r14
leaq 0xb0(%rsp), %r15
cmpq %r13, %r12
je 0x1899
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %rbx, 0xb0(%rsp)
movq %rbp, 0xb8(%rsp)
movq %r14, %rdi
movq %r15, %rcx
callq 0x19cd
movq %rbx, %rdi
callq 0x1070
addq $0x18, %r12
jmp 0x1862
leaq 0xc0(%rsp), %rdi
callq 0x1ee8
leaq 0x80(%rsp), %rdi
callq 0x19ec
leaq 0x28(%rsp), %rdi
callq 0x202c
xorl %ebx, %ebx
jmp 0x18d0
leaq 0x17c8(%rip), %rdi      # 0x3090
callq 0x1140
pushq $0x1
popq %rbx
leaq 0x60(%rsp), %rdi
callq 0x1d08
jmp 0x145f
movq %rax, %rbx
jmp 0x1907
jmp 0x1921
jmp 0x18ea
jmp 0x18ea
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1ee8
leaq 0x80(%rsp), %rdi
callq 0x19ec
leaq 0x28(%rsp), %rdi
callq 0x202c
jmp 0x1924
movq %rax, %rbx
jmp 0x1938
jmp 0x191c
jmp 0x191c
movq %rax, %rbx
jmp 0x192e
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x1d08
leaq 0x40(%rsp), %rdi
callq 0x19ec
leaq 0x10(%rsp), %rdi
callq 0x19ec
movq %rbx, %rdi
callq 0x1190