Under construction

Still being built. Things may change

Peaks

Time limit1sMemory limit128 MB

Summary
Given an elevation grid, find every peak flat-region and for each compute the maximum possible minimum elevation on a path leading to some strictly higher peak, using union-find over sorted elevations.
Level

Hard8 of 10

Topics
Union-find, Sorting, Graph
Solved
No attempts yet

Problem

An alpinist who lives on a mountainous island has climbed to some peak and now wants to reach a higher one.

More precisely, every point on the island has a positive elevation above sea level (the sea has elevation 00). If the peak the alpinist currently stands on has elevation EiE_i, his goal is to reach some peak with elevation Ej>EiE_j > E_i. Because he is already on a peak, there is no path leading immediately uphill: to reach a higher point he must first descend to some lower level and only then climb up again. The descent is never as rewarding as the climb, so the alpinist wants to maximize the elevation of the lowest point on the path from his current location to the higher peak.

For example, suppose the profile of the island is as shown in the figure and the alpinist stands on the peak with elevation E4E_4. There are three higher peaks (E5E_5, E6E_6, and E7E_7), but the path whose lowest point is as high as possible is the path to the peak with elevation E7E_7 — along this path he never descends below level E2E_2 (on the other paths he is forced down to level E1E_1). Had he started from E5E_5, the corresponding lowest level would be E3E_3 (the path to E6E_6); had he started from E6E_6, it would be E1E_1.

The map of the island is a two-dimensional rectangular table of N×MN \times M squares describing the elevation of the individual regions of the island: the number in a cell gives the elevation of the corresponding region. Two cells are adjacent if they share at least one common point, so every cell (except those on the border) is adjacent to eight others. A path is a sequence of cells in which every two consecutive cells are adjacent. A flat area is a set of one or more cells of equal elevation such that any two of them are connected by a path visiting only cells of the set; any two adjacent cells of equal elevation belong to the same flat area. A peak is a flat area none of whose cells has an adjacent cell of higher elevation.

Write a program that finds every peak on the island and, for each of them, the highest possible elevation of the lowest point on a path to some peak of higher elevation. For the highest peak on the island (the one that has no higher peak) we assume the alpinist leaves the island in search of higher peaks, so its lowest point is 00 (sea level).

Input

The first line contains two positive integers NN and MM (1≤N,M≤20001 \le N, M \le 2000, N×M≤105N \times M \le 10^5) — the height and the width of the map. Each of the next NN lines contains MM integers EijE_{ij} (1≤Eij≤1061 \le E_{ij} \le 10^6) separated by single spaces. The value EijE_{ij}, given as the jj-th number on the (i+1)(i+1)-th line, is the elevation of the cell in row ii and column jj.

Output

On the first line print one integer PP — the number of peaks on the island. Each of the next PP lines must contain two integers: the elevation of a peak and the highest possible elevation of the lowest point on a path from that peak to some higher peak. Print the peaks in descending order of their elevation; peaks of equal elevation must be printed in descending order of the elevation of their lowest point.

Hint

All peaks are marked with circles. One possible path starting from the peak with elevation 1515 is shown in a darker shade.

Examples4

  1. Example 1

    Input
    6 6
    21 16 9 11 6 7
    21 21 10 14 15 9
    18 20 8 9 13 14
    11 10 9 9 8 13
    8 12 12 14 13 8
    7 13 12 9 5 1
    
    Expected output
    4
    21 0
    15 11
    14 13
    13 12
    
  2. Example 2

    Input
    5 3
    16 14 16
    14 14 15
    12 17 16
    12 13 10
    16 11 16
    
    Expected output
    5
    17 0
    16 15
    16 14
    16 13
    16 13
    
  3. Example 3

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

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