Pineapple Farming

On a grid of heights with all outside cells treated as height 0, find the largest connected set of cells that forms a puddle bounded by strictly taller neighbors for some threshold h.

Hard8Union-findBFSDFSSimulationNo attempts yetTime limit2sMemory limit256 MB

Problem

Pineapples are tropical fruit that need heavy rain to grow. Byeongchan bought an entire mountain in a rainy tropical area to put more pineapple on pizza.

He wants to plant pineapples on all of the land, but a problem came up. Very heavy rain can leave a huge puddle on the mountain, and pineapples under water cannot be grown. Byeongchan wants to find the largest puddle on this mountain.

A set of cells is a puddle when it satisfies all of the following conditions.

  1. The mountain has WW columns and HH rows, and each cell has an integer height from 11 to 10910^9. Every height outside the mountain is 00.

  2. The cells of one puddle are connected. Connected means each cell reaches every other cell by moves to the four neighboring cells in the up, down, left, and right directions, and diagonal contact does not count as connected.

  3. There is an integer hh such that:

    • Every cell of the puddle has height at most hh.
    • Every cell outside the puddle and adjacent to the puddle in one of the four directions has height at least h+1h+1.

Input

The first line gives HH and WW, the number of rows and columns of the mountain. (1H,W10001 \le H, W \le 1000)

Each of the next HH lines gives WW numbers.

The jjth number on line i+1i+1 (1iH1 \le i \le H) is the height of the cell in row ii and column jj.

Output

Print the size of the largest puddle on the mountain, measured as the number of cells. If there is no puddle, print 00.

Hint

The shaded area in the following arrangement is the largest puddle.

87668
98549
72375
95623
99641