Boundless Boxes

Time limit1sMemory limit128 MB

Summary
Given a grid and up to 1000 seed cells, find the largest Chebyshev distance from any cell to its nearest seed, plus one.
Level

Medium4 of 10

Topics
Geometry, Brute force, Array
Solved
No attempts yet

Problem

Remember the painter Peer from the 2008 ACM ICPC World Finals? Peer was one of the inventors of monochromy, meaning that each of his paintings uses a single color but in several different shades. He also favors simple geometric forms.

Several months ago, Peer painted triangles on a canvas from the outside in. Now that triangles are out and squares are in, his newest paintings use concentric squares drawn from the inside out! Peer starts with a rectangular canvas divided into a perfect square grid. He picks some single grid cells to act as central seeds and paints them with the darkest shade. From each seed he paints a larger square, one shade lighter, that encloses it, and keeps enclosing it with ever larger squares until the whole canvas is covered. Each square is exactly one grid cell larger, and one shade lighter, than the one it encloses. When squares overlap, the cell is always filled with the darker shade.

Figure 1: Example of one of Peer's most recent works, using six shades of color.

Equivalently, a cell's shade number is 11 plus its Chebyshev distance to the nearest seed: if a cell lies at distance d=max⁡(∣r−ri∣, ∣c−ci∣)d = \max(|r - r_i|,\ |c - c_i|) from its closest seed (ri,ci)(r_i, c_i), its shade number is d+1d + 1, with the darkest shade being 11. The number of shades required is the largest shade number that appears anywhere on the canvas.

Given the size of the canvas and the locations of the seeds, write a program that computes the number of shades needed for the painting.

Input

The input contains multiple test cases. Each test case begins with one line of three space-separated integers mm, nn, and ss. The canvas has exactly m×nm \times n grid cells (1≤m,n≤10001 \le m, n \le 1000); rows are numbered 11 to mm vertically and columns 11 to nn horizontally. The painting uses ss seed cells (1≤s≤10001 \le s \le 1000), given on the next ss lines. Each of those lines contains two integers rir_i and cic_i (1≤ri≤m1 \le r_i \le m, 1≤ci≤n1 \le c_i \le n): the row and column of one seed cell. Every seed lies within the canvas.

A blank line separates consecutive test cases. A line containing 0 0 0 marks the end of the input and must not be processed.

Output

For each test case, print a single line containing one integer: the number of different shades required for the described painting.

Examples3

  1. Example 1

    Input
    10 8 3
    3 3
    7 7
    10 2
    
    2 2 1
    1 2
    
    0 0 0
    
    Expected output
    6
    2
    
  2. Example 2

    Input
    5 5 1
    1 1
    0 0 0
    
    Expected output
    5
    
  3. Example 3

    Input
    5 5 1
    3 3
    0 0 0
    
    Expected output
    3