This page is still under construction.

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

Texture Tile

Time limit2sMemory limit256 MB

Summary
Given an N x N image, find the largest square subimage whose top row equals its bottom row and left column equals its right column.
Level

Medium7 of 10

Topics
Dynamic programming, Hash map, Binary search
Solved
No attempts yet

Problem

A square raster image is represented by an N×NN \times N array of pixels. A texture tile is a square (sub)image whose first row is identical to its last row and whose first column is identical to its last column. This property is useful when covering the surface of a graphics object with repeated copies of a texture, because it lets adjacent copies join together "seamlessly".

Given an image, find the side length of the largest square subimage that is a texture tile.

Input

The first token is the integer NN. It is followed by N2N^2 integers ci,jc_{i,j}, the pixel values, given row by row (row 11 first, then row 22, and so on).

Output

Print a single integer mm: the side length of the largest texture tile contained in the image. (Since a single pixel is always a texture tile, m≥1m \ge 1 always holds.)

Constraints

  • 1≤N≤3701 \le N \le 370
  • 0≤ci,j≤2550 \le c_{i,j} \le 255

Examples4

  1. Example 1

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

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

    Input
    1
    5
    
    Expected output
    1
    
  4. Example 4

    Input
    2
    5 5
    5 5
    
    Expected output
    2