This page is still under construction.

Parts of this page are still being built. What you see may change.

Mattress stain removal

Time limit3sMemory limit256 MB

Summary
Cover all stained cells on an m by n grid with the fewest 3 by 3 blocks placed inside the grid.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Hongjun threw a party at his house over the Chuseok holiday. Many small children came, and the next morning he found several stains that had soaked through the bed sheet into the mattress. Washing the whole mattress costs too much, so Hongjun wants to buy a small stain removal tool instead.

The top surface of the mattress is a rectangular grid with mm rows and nn columns. The top left cell is (1,1)(1, 1) and the bottom right cell is (m,n)(m, n). The coordinate (r,s)(r, s) means the cell in row rr from the top and column ss from the left. One stain covers one cell.

The tool takes a 3×33 \times 3 block of cells that lies entirely inside the mattress and erases every stain in that block at once. The sides of the block are parallel to the sides of the mattress. The tool is thrown away after one use, so one tool clears one block. Given the size of the mattress and the positions of the stains, find the smallest number of tools needed to erase every stain.

On a 6×116 \times 11 mattress with stains at (4,3)(4, 3), (6,5)(6, 5), (3,6)(3, 6), (4,7)(4, 7), two tools are enough. The block spanning rows 4 to 6 and columns 3 to 5 erases (4,3)(4, 3) and (6,5)(6, 5), and the block spanning rows 2 to 4 and columns 5 to 7 erases (3,6)(3, 6) and (4,7)(4, 7).

Input

The first line contains the number of test cases TT. The TT test cases follow.

The first line of each test case contains two natural numbers mm and nn, the size of the mattress. (3≤m≤103 \le m \le 10, 3≤n≤10003 \le n \le 1000) The second line contains the number of stains cc. (0≤c≤mn0 \le c \le mn) Each of the next cc lines contains the row number and the column number of one stain, separated by a space. The row number is between 11 and mm, the column number is between 11 and nn, and no two stains share a coordinate.

Output

For each test case, print the smallest number of tools needed to erase every stain, one number per line.

Examples2

  1. Example 1

    Input
    2
    6 11
    4
    4 3
    6 5 
    3 6
    4 7 
    7 7
    4 
    3 2
    5 6
    6 4
    7 6
    
    Expected output
    2
    2
    
  2. Example 2

    Input
    2
    3 3
    0
    3 3
    9
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    3 1
    3 2
    3 3
    
    Expected output
    0
    1