This page is still under construction.

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

Hole

Interview

Time limit2sMemory limit512 MB

Summary
Find the side length of the largest all-zero square in an n by n binary grid given by the positions of its ones.
Level

Medium5 of 10

Topics
Dynamic programming, Matrix
Solved
No attempts yet

Problem

A group of scientists want to monitor a huge forest. They plan to airdrop small sensors into the forest. Many conditions during the airdrop are unpredictable, so each sensor lands in a random location. After all sensors have landed, square regions that contain no sensor remain in the forest. Call such a region a hole.

Small holes are desirable, and airdropping a very large number of sensors achieves that. Sensors are expensive, though. So the scientists want to run a computer simulation to decide how many sensors to airdrop, so that the chance of a large hole is small. The simulation needs a subroutine that reads the locations of the sensors and outputs the size of the largest hole. The simulation repeats many times with different parameters, so the subroutine has to be fast. Writing that subroutine is your task.

Consider an n×nn \times n array AA of 0s and 1s. There is a hole in AA at (x,y)(x, y) with width dd when both of the following hold.

  • A[i,j]=0A[i, j] = 0 for every (i,j)(i, j) with i=x,x+1,x+2,…,x+d−1i = x, x+1, x+2, \dots, x+d-1 and j=y,y+1,y+2,…,y+d−1j = y, y+1, y+2, \dots, y+d-1.
  • x+d−1<nx + d - 1 < n and y+d−1<ny + d - 1 < n.

That is, every value inside the square at (x,y)(x, y) with width dd is 0. The indices of the array start from 0.

Given the array AA, find the width of the largest hole.

Input

The input represents the array AA.

The first line contains the width of the array nn, a positive integer of at most 1,024. The second line contains an integer kk, the number of 1s in the array. Then kk lines follow, one for each entry with value 1. Each line contains two integers xx and yy, which means A[x,y]=1A[x, y] = 1.

Since nn may be as large as 1,024, your program has to run fast enough to handle arrays of that size.

Output

Print one integer, the width of the largest hole. If the array has no hole, print 0.

Hint

The figure below shows the array of the first example. The entry A[i,j]A[i, j] sits at row ii and column jj, and the arrow points to the location of A[3,7]A[3, 7].

In this array there is a hole of width 3 at (0,0)(0, 0), and no hole of width 4 at (0,0)(0, 0). The largest hole has width 5 and sits at (1,2)(1, 2). The highlighted square in the figure is that hole.

Several fast methods find the largest hole. Below are hints to two different methods.

Hint 1: Consider these two values.

  • d1d_1: the width of the largest hole that fits at (1,1)(1, 1)
  • d2d_2: the width of the largest hole that fits at (2,2)(2, 2)

What is the relationship between d1d_1 and d2d_2? If you know d2d_2, is there a fast way to compute d1d_1?

Hint 2: Consider these two values.

  • s1s_1: the number of 1s inside the square at (0,0)(0, 0) with width 7
  • s2s_2: the number of 1s inside the square at (0,1)(0, 1) with width 7

What is the relationship between s1s_1 and s2s_2? If you know s1s_1, is there a fast way to compute s2s_2?

Examples2

  1. Example 1

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

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