cswork / OAOpen practice workspace

AmazonOA #176

Find Minimum Time to Create Beautiful Canvas

Estimated MediumJudge-ready

Topics (AI-labelled)Binary SearchPrefix & Ranges

On a white n×m canvas, one given cell is painted black every minute, and each cell appears exactly once. Find the minute at which a fully painted k×k square first exists.

Input

The first line contains n m k, followed by n×m lines of 1-indexed coordinates, all distinct. 1≤n,m≤750; 1≤k≤min(n,m).

Output

Output a single integer, the answer.

Example 1

Input:

1 1 1
1 1

Output:

1

Example 2

Input:

2 2 2
1 1
2 2
1 2
2 1

Output:

4

Example 3

Input:

2 3 2
1 1
1 2
2 1
2 2
1 3
2 3

Output:

4

Example notes

The answers to the three examples are 1, 4 and 4; in the third example the left square is completed first.

Editorial

Loading solution…