// File: /workspace/github_cpp_makefile_star2_O3/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 0x3c34(%rip), %rdi      # 0x4fd8
leaq 0x4(%rsp), %rbx
movq %rbx, %rsi
callq 0x10b0
movq %rsp, %rsi
movq %rax, %rdi
callq 0x10b0
movslq (%rbx), %rsi
testq %rsi, %rsi
jle 0x1723
cmpl $0x0, (%rsp)
jle 0x1723
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x1c7c
movslq 0x4(%rsp), %rsi
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1c7c
movl 0x4(%rsp), %r14d
movl (%rsp), %r11d
testl %r14d, %r14d
jle 0x17b9
xorl %r15d, %r15d
leaq (%r15,%r15,2), %r12
leaq (,%r12,8), %rbx
movq 0x40(%rsp), %rdi
addq %rbx, %rdi
movslq %r11d, %rsi
callq 0x1cfa
addq 0x70(%rsp), %rbx
movslq (%rsp), %rsi
movq %rbx, %rdi
callq 0x1cfa
movl (%rsp), %r11d
testl %r11d, %r11d
jle 0x14d9
leaq -0x1(%r15), %rbx
movq 0x40(%rsp), %rax
movq (%rax,%r12,8), %rsi
leaq (%rbx,%rbx,2), %r13
xorl %ebp, %ebp
xorl %r14d, %r14d
addq %rbp, %rsi
movq 0x3b74(%rip), %rdi      # 0x4fd8
callq 0x10b0
movq 0x40(%rsp), %rax
movq (%rax,%r12,8), %rsi
movl (%rsi,%rbp), %eax
testl %eax, %eax
js 0x176c
xorl %edi, %edi
movq 0x70(%rsp), %rcx
movl $0x0, %edx
testq %rbp, %rbp
je 0x1496
movq (%rcx,%r12,8), %rdx
movl -0x4(%rdx,%rbp), %edx
testq %r15, %r15
je 0x14a2
movq (%rcx,%r13,8), %rdi
movl (%rdi,%rbp), %edi
leaq -0x1(%r14), %r9
xorl %r8d, %r8d
orl %ebx, %r9d
js 0x14b7
movq (%rcx,%r13,8), %r8
movl -0x4(%r8,%rbp), %r8d
cmpl $0x1, %eax
sbbl $-0x1, %edx
addl %edi, %edx
subl %r8d, %edx
movq (%rcx,%r12,8), %rax
movl %edx, (%rax,%rbp)
incq %r14
movslq (%rsp), %r11
addq $0x4, %rbp
cmpq %r11, %r14
jl 0x145a
incq %r15
movslq 0x4(%rsp), %r14
cmpq %r14, %r15
jl 0x140a
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
testl %r14d, %r14d
jle 0x17cc
xorl %edi, %edi
movl %r11d, %ecx
testl %ecx, %ecx
jle 0x1701
leaq (%rdi,%rdi,2), %rax
movq %rax, 0x88(%rsp)
xorl %ebx, %ebx
movq %rdi, 0xb0(%rsp)
movq 0x40(%rsp), %rax
movq 0x88(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
je 0x16ea
leal 0x1(%rcx), %esi
cltd
idivl %esi
cmpl %r14d, %eax
jge 0x16ea
movl %eax, %esi
leal 0x1(%rsi), %r9d
movq 0x40(%rsp), %rax
movq 0x88(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movl (%rax,%rbx,4), %eax
cltd
idivl %r9d
testl %edx, %edx
movl %r9d, 0x38(%rsp)
je 0x1581
movl 0x38(%rsp), %eax
jmp 0x154b
movl %eax, %r13d
movl %edi, %edx
subl %r9d, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovsl %eax, %edx
movslq %edx, %rax
cmpq %rax, %rdi
jle 0x157b
leal (%rdx,%rsi), %r12d
incl %r12d
cmpl %r14d, %r12d
jge 0x157b
incl %edx
movl %ebx, %esi
subl %r13d, %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovsl %eax, %esi
addl %esi, %r13d
movl %esi, 0x30(%rsp)
movslq %esi, %rsi
movl %r13d, 0x3c(%rsp)
movq %rsi, 0xb8(%rsp)
cmpq %rsi, %rbx
jle 0x16cc
cmpl %ecx, %r13d
jge 0x16cc
movl %r12d, %r14d
movq %rdx, 0x8(%rsp)
leal -0x1(%rdx), %r15d
movl 0x30(%rsp), %ebp
xorl %eax, %eax
movl %r13d, %edx
orl %r12d, %edx
leaq (%r14,%r14,2), %rsi
movl %r13d, %r8d
movl $0x0, %edx
js 0x1610
movq 0x70(%rsp), %rdx
movq (%rdx,%rsi,8), %rdx
movl (%rdx,%r8,4), %edx
movl %r13d, %edi
orl %r15d, %edi
leaq (%r15,%r15,2), %rdi
js 0x1629
movq 0x70(%rsp), %rax
movq (%rax,%rdi,8), %rax
movl (%rax,%r8,4), %eax
xorl %r8d, %r8d
movl %ebp, %r9d
orl %r12d, %r9d
movl %ebp, %r10d
movl $0x0, %r9d
js 0x164a
movq 0x70(%rsp), %r9
movq (%r9,%rsi,8), %rsi
movl (%rsi,%r10,4), %r9d
movl %ebp, %esi
orl %r15d, %esi
js 0x165e
movq 0x70(%rsp), %rsi
movq (%rsi,%rdi,8), %rsi
movl (%rsi,%r10,4), %r8d
incl %ebp
addl %r9d, %eax
subl %eax, %edx
addl %r8d, %edx
cmpl $0x1, %edx
jne 0x169a
movl %r13d, 0x58(%rsp)
movl %ebp, 0x5c(%rsp)
movl %r12d, 0x60(%rsp)
movq 0x8(%rsp), %rax
movl %eax, 0x64(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1f14
movl (%rsp), %r11d
movl %r11d, %ecx
movl %ebp, %eax
cmpq %rax, %rbx
jbe 0x16ad
incl %r13d
cmpl %ecx, %r13d
jl 0x15ed
movl 0x4(%rsp), %r14d
movq 0xb0(%rsp), %rdi
movl 0x3c(%rsp), %r13d
movq 0x8(%rsp), %rdx
movq 0xb8(%rsp), %rsi
movl %edx, %eax
cmpq %rax, %rdi
jbe 0x157b
incl %edx
incl %r12d
cmpl %r14d, %r12d
jl 0x15cb
jmp 0x157b
incq %rbx
movslq %r11d, %rdx
movl %r11d, %eax
movl %r11d, %ecx
cmpq %rdx, %rbx
jl 0x1529
jmp 0x1703
movl %ecx, %eax
incq %rdi
movslq %r14d, %rdx
movl %eax, %ecx
cmpq %rdx, %rdi
jl 0x150b
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r13
jmp 0x17d1
movq 0x38c6(%rip), %rbx      # 0x4ff0
leaq 0x190f(%rip), %rsi      # 0x3040
movl $0x12, %edx
movq %rbx, %rdi
callq 0x1130
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10d0
xorl %ebx, %ebx
jmp 0x1bdf
movq 0x387d(%rip), %rbx      # 0x4ff0
leaq 0x18c6(%rip), %rsi      # 0x3040
movl $0x12, %edx
movq %rbx, %rdi
callq 0x1130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq 0x384b(%rip), %rdi      # 0x4ff0
callq 0x1040
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x10d0
jmp 0x1bcb
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r13, %rcx
subq %rbx, %rcx
sarq $0x4, %rcx
movq %r11, %rbp
imull %r11d, %r14d
movl %r14d, %eax
movq %rcx, 0x8(%rsp)
imull %ecx, %eax
movslq %eax, %r12
movq 0x37b9(%rip), %rsi      # 0x4fb0
movq %r12, %rdi
callq 0x11a0
testq %rax, %rax
je 0x1ba3
movq %rax, %r15
movq %r13, 0x30(%rsp)
xorl %r13d, %r13d
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x10a0
cmpq %rbx, 0x30(%rsp)
je 0x189f
movq 0x8(%rsp), %rax
cmpq $0x1, %rax
adcq $0x0, %rax
xorl %ecx, %ecx
movq %rcx, %r8
shlq $0x4, %r8
movl 0x8(%rbx,%r8), %edx
movl 0xc(%rbx,%r8), %esi
cmpl %edx, %esi
jg 0x1894
addq %rbx, %r8
movl (%r8), %edi
movl 0x4(%r8), %r8d
movl %ebp, %r9d
imull %esi, %r9d
addl %r8d, %r9d
addl %r13d, %r9d
movl %edi, %r10d
subl %r8d, %r10d
incl %r10d
movl %r10d, %r11d
movl %r9d, %r12d
cmpl %edi, %r8d
jg 0x1886
movslq %r12d, %r12
movb $0x1, (%r15,%r12)
incl %r12d
decl %r11d
jne 0x1876
leal 0x1(%rsi), %r11d
addl %ebp, %r9d
cmpl %edx, %esi
movl %r11d, %esi
jne 0x186b
incq %rcx
addl %r14d, %r13d
cmpq %rax, %rcx
jne 0x1836
leaq 0x58(%rsp), %rdi
callq 0x20f0
leaq 0x58(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x21e6
pxor %xmm0, %xmm0
leaq 0x90(%rsp), %rsi
movdqa %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0xc0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movdqa %xmm0, (%rdx)
leaq 0x58(%rsp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x2510
movq 0x90(%rsp), %rcx
movq 0x98(%rsp), %rax
subq %rcx, %rax
cmpq $0x18, %rax
je 0x195f
movq 0x36d8(%rip), %rbx      # 0x4ff0
leaq 0x1734(%rip), %rsi      # 0x3053
movl $0x11, %edx
movq %rbx, %rdi
callq 0x1130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq 0x36a6(%rip), %rdi      # 0x4ff0
callq 0x1040
movq %rax, %rdi
callq 0x10d0
movq 0x90(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
je 0x1ad2
sarq $0x2, %rcx
movq 0x10(%rsp), %rdx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %esi, %esi
movdqa 0x1686(%rip), %xmm0     # 0x3010
movdqa 0x168e(%rip), %xmm1     # 0x3020
movdqa 0x1696(%rip), %xmm2     # 0x3030
pcmpeqd %xmm3, %xmm3
movslq (%rax,%rsi,4), %r10
shlq $0x4, %r10
movl 0x8(%rdx,%r10), %edi
movslq 0xc(%rdx,%r10), %r8
cmpl %edi, %r8d
jg 0x1ac6
addq %rdx, %r10
movslq 0x4(%r10), %r9
movl (%r10), %r10d
movq 0x40(%rsp), %r11
incl %edi
movl %r10d, %r14d
subl %r9d, %r14d
leaq 0x4(%r14), %rbx
andq $-0x4, %rbx
movq %r14, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
movq %r9, %r14
shlq $0x2, %r14
pxor %xmm2, %xmm4
cmpl %r9d, %r10d
jl 0x1aba
leaq (%r8,%r8,2), %r15
movq (%r11,%r15,8), %r15
addq %r14, %r15
addq $0xc, %r15
xorl %r12d, %r12d
movq %r12, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm1, %xmm6
pxor %xmm2, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm4, %xmm7
pcmpeqd %xmm4, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %ebp
notl %ebp
testb $0x1, %bpl
je 0x1a50
movl %esi, -0xc(%r15,%r12,4)
pxor %xmm3, %xmm6
pextrw $0x4, %xmm6, %ebp
testb $0x1, %bpl
je 0x1a64
movl %esi, -0x8(%r15,%r12,4)
por %xmm0, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm4, %xmm6
pcmpeqd %xmm4, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
pxor %xmm3, %xmm5
pextrw $0x0, %xmm5, %ebp
testb $0x1, %bpl
je 0x1a9e
movl %esi, -0x4(%r15,%r12,4)
pextrw $0x4, %xmm5, %ebp
testb $0x1, %bpl
je 0x1aad
movl %esi, (%r15,%r12,4)
addq $0x4, %r12
cmpq %r12, %rbx
jne 0x1a08
incq %r8
cmpl %r8d, %edi
jne 0x19ed
incq %rsi
cmpq %rcx, %rsi
jne 0x199e
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %r13
cmpq %r13, %r12
je 0x1b6b
movq 0x34dc(%rip), %rbx      # 0x4fc8
leaq 0xd8(%rsp), %r14
movq (%r12), %r15
movq 0x8(%r12), %rbp
movq %rbx, 0xd8(%rsp)
leaq 0x1559(%rip), %rax      # 0x3065
movq %rax, 0xe0(%rsp)
subq %r15, %rbp
sarq $0x2, %rbp
testq %rbp, %rbp
jle 0x1b3b
incq %rbp
movq %r14, %rdi
movq %r15, %rsi
callq 0x20b0
addq $0x4, %r15
decq %rbp
cmpq $0x1, %rbp
ja 0x1b23
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10d0
addq $0x18, %r12
cmpq %r13, %r12
jne 0x1af4
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1b88
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0x90(%rsp), %rdi
callq 0x1d2c
leaq 0x58(%rsp), %rdi
callq 0x213c
xorl %ebx, %ebx
jmp 0x1bb4
leaq 0x1522(%rip), %rdi      # 0x30cc
callq 0x1170
movl $0x1, %ebx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1bcb
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0x70(%rsp), %rdi
callq 0x1d2c
leaq 0x40(%rsp), %rdi
callq 0x1d2c
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1c39
jmp 0x1c45
jmp 0x1c0c
movq %rax, %rbx
jmp 0x1c69
jmp 0x1c07
jmp 0x1c07
jmp 0x1c0c
movq %rax, %rbx
jmp 0x1c5f
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1c2c
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0x90(%rsp), %rdi
callq 0x1d2c
leaq 0x58(%rsp), %rdi
callq 0x213c
jmp 0x1c48
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1c5f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0x70(%rsp), %rdi
callq 0x1d2c
leaq 0x40(%rsp), %rdi
callq 0x1d2c
movq %rbx, %rdi
callq 0x11c0
nop