// File: /workspace/github_cpp_makefile_star2_O2/kmyk[P]topcoder-marathon-match-100-same-color-pairs/./SameColorPairs.cpp
// Function: solve(int, int, int, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>> const&)
vector<array<int, 4> > solve(int H, int W, int C, vector<vector<int> > const & original_board) {
    // assume H <= W
    if (H > W) {
        return with_transpose(H, W, C, original_board, [&](int H, int W, int C, vector<vector<int> > board) {
            return solve(H, W, C, board);
        });
    }

    double clock_begin = rdtsc();
    random_device device;
    xor_shift_128 gen(device());
    vector<vector<int> > board = original_board;

    // split the board to blocks
    int hk = H / 30 + 1;
    int wk = W / 30 + 1;
    while (hk * wk >= 2 and (H / hk) * (W / wk) <= 400) {
        -- (hk >= wk ? hk : wk);  // not to make too small blocks
    }
    int k = hk * wk;
    vector<int> h = split_to_chunks(H, hk);
    vector<int> w = split_to_chunks(W, wk);

    // define the ordering
    vector<tuple<int, int, int, int> > order;
    assert (hk <= hk);  // since H <= W
    REP (y, hk) REP (x, wk) {
        int a = y;
        int b = (y % 2 == 0 ? - x : x);
        if (hk == 1) {
            // nop
        } else if (hk == 2) {
            if (y == 0 and x == wk - 1) a = 100;
        } else if (hk == 3) {
            // nop
        } else if (hk == 4) {
            assert (wk == 4);
            if (x < wk - 1) {
                // nop
            } else {
                a = 100;
                b = - y;
            }
        } else {
            assert (false);
        }
        order.emplace_back(a, b, y, x);
    }
    sort(ALL(order));
    order.pop_back();

    // solve each block
    vector<array<int, 4> > rects;
    vector<vector<bool> > used(hk, vector<bool>(wk));
    REP (i, k - 1) {
        int y, x; tie(ignore, ignore, y, x) = order[i];
        cerr << "[" << h[y] << ", " << h[y + 1] << ") * [" << w[x] << ", " << w[x + 1] << ")" << endl;

        // exntend the area
        used[y][x] = true;
        int ly = y, ry = y + 1;
        int lx = x, rx = x + 1;
        while (lx - 1 >=  0 and used[y][lx - 1]) -- lx;
        while (rx     <  wk and used[y][rx]) ++ rx;
        while (ly - 1 >=  0) {
            bool valid = true;
            REP3 (x, lx, rx) if (not used[ly - 1][x]) valid = false;
            if (not valid) break;
            -- ly;
        }
        while (ry < hk) {
            bool valid = true;
            REP3 (x, lx, rx) if (not used[ry][x]) valid = false;
            if (not valid) break;
            ++ ry;
        }

        // run greedy
        vector<array<int, 4> > delta =
            with_crop(H, W, C, board, h[ly], w[lx], h[ry], w[rx], [&](int H, int W, int C, vector<vector<int> > const & board) {
                return with_compress(H, W, C, board, [&](int H, int W, int C, vector<vector<int> > const & board) {
                    return with_landscape(H, W, C, board, [&](int H, int W, int C, vector<vector<int> > const & board) {
                        double clock_end = min(rdtsc() + min(1.0, 3 * 5.0 / (k - 1)), clock_begin + 5.0 * (i + 1) / (k - 1));
                        return solve_sa_greedy(H, W, C, board, clock_end, gen);
                    });
                });
            });
        apply_rects(board, ALL(delta));
        copy(ALL(delta), back_inserter(rects));
    }
    // solve the entire board
    vector<array<int, 4> > delta =
        with_compress(H, W, C, board, [&](int H, int W, int C, vector<vector<int> > const & board) {
            return with_landscape(H, W, C, board, [&](int H, int W, int C, vector<vector<int> > const & board) {  // use landscape mode to make linked lists more efficient
                return solve_sa_greedy(H, W, C, board, clock_begin + 9.5, gen);
            });
        });
    apply_rects(board, ALL(delta));
    copy(ALL(delta), back_inserter(rects));
    return rects;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1518, %rsp           # imm = 0x1518
movq %r8, %rbx
movq %rdi, %r14
cmpl %edx, %esi
jle 0x2d39
xorpd %xmm0, %xmm0
leaq 0x150(%rsp), %r9
movapd %xmm0, (%r9)
leaq 0x983(%rip), %rax       # 0x3690
movq %rax, 0x18(%r9)
leaq 0x9e2(%rip), %rax       # 0x36fa
movq %rax, 0x10(%r9)
movq %r14, %rdi
movq %rbx, %r8
callq 0x2784
leaq 0x150(%rsp), %rdi
callq 0x3d16
jmp 0x3413
movl %esi, 0x2c(%rsp)
movl %edx, 0x20(%rsp)
movl %ecx, 0x30(%rsp)
movq %r14, 0x10(%rsp)
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
movq %rax, %xmm0
punpckldq 0x52ae(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x52b6(%rip), %xmm0     # 0x8020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x52c2(%rip), %xmm1     # 0x8040
movsd %xmm1, 0x98(%rsp)
leaq 0x190(%rsp), %r14
movq %r14, %rdi
callq 0x3d32
movq %r14, %rdi
callq 0x2210
leaq 0x140(%rsp), %rdi
movl %eax, %esi
callq 0x45b0
leaq 0x128(%rsp), %rdi
movq %rbx, %rsi
callq 0x3d88
pushq $0x1e
popq %rcx
movl 0x2c(%rsp), %r8d
movl %r8d, %eax
cltd
idivl %ecx
movl %eax, %ebx
incl %ebx
movl 0x20(%rsp), %edi
movl %edi, %eax
cltd
idivl %ecx
movl %eax, %r14d
incl %r14d
movl %ebx, %esi
imull %r14d, %esi
cmpl $0x2, %esi
jl 0x2e19
movl %r8d, %eax
cltd
idivl %ebx
movl %eax, %ecx
movl %edi, %eax
cltd
idivl %r14d
imull %ecx, %eax
cmpl $0x190, %eax            # imm = 0x190
jg 0x2e19
cmpl %r14d, %ebx
movl %r14d, %eax
cmovgl %ebx, %eax
decl %eax
cmpl %r14d, %ebx
cmovll %eax, %r14d
cmovgel %eax, %ebx
jmp 0x2ddf
movl %esi, 0x34(%rsp)
leaq 0x110(%rsp), %rdi
movl %r8d, %esi
movl %ebx, %edx
callq 0x2c87
leaq 0xf8(%rsp), %rdi
movl 0x20(%rsp), %esi
movl %r14d, %edx
callq 0x2c87
xorpd %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, 0x38(%rsp)
leal -0x1(%r14), %eax
movl %eax, 0x18(%rsp)
xorl %eax, %eax
movq %rbx, 0x70(%rsp)
leal -0x1(%rbx), %r12d
leaq 0x5219(%rip), %rbx      # 0x8090
leaq 0xc(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0x40(%rsp), %rbp
leaq 0x80(%rsp), %r13
movl %eax, 0x40(%rsp)
cmpl 0x70(%rsp), %eax
jge 0x2f3b
xorl %eax, %eax
movl %eax, 0x80(%rsp)
cmpl 0x38(%rsp), %eax
jge 0x2f30
movl 0x40(%rsp), %ecx
movl %ecx, 0xc(%rsp)
movl %eax, %edx
negl %edx
testb $0x1, %cl
cmovnel %eax, %edx
movl %edx, 0x28(%rsp)
cmpl $0x3, %r12d
ja 0x3428
movslq (%rbx,%r12,4), %rdx
addq %rbx, %rdx
jmpq *%rdx
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbp, %rcx
movq %r13, %r8
callq 0x3de2
movl 0x80(%rsp), %eax
incl %eax
jmp 0x2e9e
testl %ecx, %ecx
jne 0x2ed8
cmpl 0x18(%rsp), %eax
jne 0x2ed8
movl $0x64, 0xc(%rsp)
jmp 0x2ed8
cmpl $0x4, 0x38(%rsp)
jne 0x3447
cmpl $0x3, %eax
jl 0x2ed8
movl $0x64, 0xc(%rsp)
negl %ecx
movl %ecx, 0x28(%rsp)
jmp 0x2ed8
movl 0x40(%rsp), %eax
incl %eax
jmp 0x2e8e
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x4f24
addq $-0x10, 0xa8(%rsp)
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %r14
movupd %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movslq 0x38(%rsp), %rsi
leaq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rsi, 0xf0(%rsp)
callq 0x3e30
movslq 0x70(%rsp), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %rsi, 0xe8(%rsp)
callq 0x3e44
leaq 0x40(%rsp), %rdi
callq 0x3cc0
xorl %eax, %eax
leaq 0x52f8(%rip), %rbx      # 0x82b7
movl %eax, 0xc(%rsp)
movl 0x34(%rsp), %ecx
decl %ecx
cmpl %ecx, %eax
jge 0x3332
cltq
movq 0xa0(%rsp), %rcx
shlq $0x4, %rax
movslq 0x4(%rcx,%rax), %r15
movl (%rcx,%rax), %eax
movq %rax, 0x18(%rsp)
movq 0x9ffd(%rip), %rdi      # 0xcff0
leaq 0x52c4(%rip), %rsi      # 0x82be
callq 0x2190
movq 0x110(%rsp), %rcx
movl (%rcx,%r15,4), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2190
movq 0x110(%rsp), %rcx
movl 0x4(%rcx,%r15,4), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
leaq 0x527d(%rip), %rsi      # 0x82ba
callq 0x2190
movslq 0x18(%rsp), %rbp
movq 0xf8(%rsp), %rcx
movl (%rcx,%rbp,4), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2190
movq 0xf8(%rsp), %rcx
movl 0x4(%rcx,%rbp,4), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
leaq 0x5224(%rip), %rsi      # 0x82a7
callq 0x2190
movq %rax, %rdi
callq 0x20a0
imulq $0x28, %r15, %r12
movq 0x80(%rsp), %rdi
addq %r12, %rdi
movq %rbp, %rsi
callq 0x3b7e
movq %r15, 0x68(%rsp)
incq %r15
movl %r15d, 0x24(%rsp)
orq %rdx, (%rax)
movq 0x18(%rsp), %rax
movl %eax, %r14d
sarl $0x1f, %r14d
andl %eax, %r14d
leaq -0x1(%rax), %r13
movl %eax, %ebx
testl %ebx, %ebx
jle 0x30f1
movq 0x80(%rsp), %rdi
addq %r12, %rdi
movq %r13, %rsi
callq 0x3b7e
decl %ebx
decq %r13
testq %rdx, (%rax)
jne 0x30cc
incl %ebx
jmp 0x30f4
movl %r14d, %ebx
movq 0x18(%rsp), %rax
leal 0x1(%rax), %r13d
movq 0x38(%rsp), %rax
cmpl %r13d, %eax
cmovgl %eax, %r13d
movq %rbp, %rcx
shlq $0x20, %rcx
incq %rbp
movq %rcx, %r14
cmpq 0xf0(%rsp), %rbp
jge 0x3156
movq 0x80(%rsp), %rdi
addq %r12, %rdi
movq %rbp, %rsi
callq 0x3b7e
movabsq $0x100000000, %rcx      # imm = 0x100000000
addq %r14, %rcx
movq 0x18(%rsp), %rsi
incl %esi
movq %rsi, 0x18(%rsp)
incq %rbp
testq %rdx, (%rax)
jne 0x3113
jmp 0x315e
movl %r13d, %eax
movq %rax, 0x18(%rsp)
movslq %ebx, %rax
movq %rax, 0x78(%rsp)
sarq $0x20, %r14
movq 0x68(%rsp), %rax
movl %eax, %r12d
movl %eax, %ecx
sarl $0x1f, %ecx
andl %eax, %ecx
movl %ecx, 0x68(%rsp)
testl %r12d, %r12d
jle 0x31bc
movl %r12d, %ebp
decq %r12
movb $0x1, %bl
movq 0x78(%rsp), %r13
cmpq %r14, %r13
jg 0x31b5
imulq $0x28, %r12, %rdi
addq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x3b7e
testq %rdx, (%rax)
setne %al
andb %al, %bl
incq %r13
jmp 0x318f
testb $0x1, %bl
jne 0x317d
jmp 0x31c0
movl 0x68(%rsp), %ebp
movq 0x70(%rsp), %rax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, 0x24(%rsp)
cmpq 0xe8(%rsp), %r15
jge 0x321b
movq %r15, %r12
movb $0x1, %bl
movq 0x78(%rsp), %r13
cmpq %r14, %r13
jg 0x320c
imulq $0x28, %r12, %rdi
addq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x3b7e
testq %rdx, (%rax)
setne %al
andb %al, %bl
incq %r13
jmp 0x31e6
leaq 0x1(%r12), %r15
testb $0x1, %bl
jne 0x31d2
movl %r12d, %edx
jmp 0x321f
movl 0x24(%rsp), %edx
movslq %ebp, %rax
movq 0x110(%rsp), %rcx
movl (%rcx,%rax,4), %ebp
movq 0xf8(%rsp), %rax
movq 0x78(%rsp), %rsi
movl (%rax,%rsi,4), %r14d
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ebx
movslq 0x18(%rsp), %rcx
movl (%rax,%rcx,4), %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xd0(%rsp)
pushq $0x20
popq %rdi
callq 0x21a0
leaq 0x40(%rsp), %r12
leaq 0x34(%rsp), %rcx
movq %rcx, (%rax)
leaq 0x98(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq 0xc(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x140(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %rax, 0xc0(%rsp)
leaq 0x46a(%rip), %rax       # 0x3712
movq %rax, 0xd8(%rsp)
leaq 0x50b(%rip), %rax       # 0x37c2
movq %rax, 0xd0(%rsp)
movq %r12, %rdi
movl 0x30(%rsp), %ecx
leaq 0x128(%rsp), %r12
movq %r12, %r8
movl %ebp, %r9d
leaq 0xc0(%rsp), %r13
pushq %r13
pushq %r15
pushq %rbx
pushq %r14
callq 0x2942
addq $0x20, %rsp
movq %r13, %rdi
callq 0x3d16
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r12, %rdi
callq 0x3e9a
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x7dcc
leaq 0x40(%rsp), %rdi
callq 0x4cd0
movl 0xc(%rsp), %eax
incl %eax
jmp 0x2fb8
leaq 0x170(%rsp), %r9
leaq 0x98(%rsp), %rax
movq %rax, (%r9)
leaq 0x140(%rsp), %rax
movq %rax, 0x8(%r9)
leaq 0x6de(%rip), %rax       # 0x3a36
movq %rax, 0x18(%r9)
leaq 0x733(%rip), %rax       # 0x3a96
movq %rax, 0x10(%r9)
leaq 0x40(%rsp), %rdi
leaq 0x128(%rsp), %r8
movl 0x2c(%rsp), %esi
movl 0x20(%rsp), %edx
movl 0x30(%rsp), %ecx
callq 0x23fc
leaq 0x170(%rsp), %rdi
callq 0x3d16
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x128(%rsp), %rdi
callq 0x3e9a
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
callq 0x7dcc
leaq 0x40(%rsp), %rdi
callq 0x4cd0
leaq 0x80(%rsp), %rdi
callq 0x3ed2
leaq 0xa0(%rsp), %rdi
callq 0x4d72
leaq 0xf8(%rsp), %rdi
callq 0x4908
leaq 0x110(%rsp), %rdi
callq 0x4908
leaq 0x128(%rsp), %rdi
callq 0x3c9e
leaq 0x190(%rsp), %rdi
callq 0x3ef4
movq %r14, %rax
addq $0x1518, %rsp           # imm = 0x1518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4e82(%rip), %rdi      # 0x82b1
leaq 0x4e16(%rip), %rsi      # 0x824c
leaq 0x4e24(%rip), %rcx      # 0x8261
movl $0x20f, %edx            # imm = 0x20F
callq 0x20f0
leaq 0x4e5b(%rip), %rdi      # 0x82a9
leaq 0x4df7(%rip), %rsi      # 0x824c
leaq 0x4e05(%rip), %rcx      # 0x8261
movl $0x207, %edx            # imm = 0x207
callq 0x20f0
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4cd0
jmp 0x3505
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
jmp 0x34e9
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3cc0
jmp 0x3517
movq %rax, %rbx
jmp 0x3517
jmp 0x34fb
movq %rax, %rbx
jmp 0x3539
movq %rax, %rbx
jmp 0x3546
jmp 0x34b0
movq %rax, %rbx
jmp 0x3553
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x3d16
jmp 0x3560
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4cd0
jmp 0x350a
jmp 0x34de
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x3d16
jmp 0x3505
jmp 0x3502
movq %rax, %rbx
jmp 0x350a
jmp 0x3502
jmp 0x3502
movq %rax, %rbx
jmp 0x351f
jmp 0x3502
movq %rax, %rbx
movq 0x10(%rsp), %r14
leaq 0x80(%rsp), %rdi
callq 0x3ed2
movq %r14, %rdi
callq 0x4cd0
leaq 0xa0(%rsp), %rdi
callq 0x4d72
leaq 0xf8(%rsp), %rdi
callq 0x4908
leaq 0x110(%rsp), %rdi
callq 0x4908
leaq 0x128(%rsp), %rdi
callq 0x3c9e
leaq 0x190(%rsp), %rdi
callq 0x3ef4
movq %rbx, %rdi
callq 0x22a0