P27243 [AtCoder] Ridge
提高+
题目描述

Problem Description

JOI Caldera is a beautiful terrain loved by many mountaineers for its scenic beauty. In particular, the views from places called ridges are spectacular.

The land of JOI Caldera is a rectangle with a north-south extent of $ H $ kilometers and an east-west extent of $ W $ kilometers. The land is divided into $H \times W$ regions, with each region being 1 kilometer square. In every region, the elevation is uniform, and the elevations of different regions are distinct.

When it rains in a region, the rainwater flows to all adjacent regions (up to 4 regions to the north, south, east, and west) that have lower elevations. If there are no such adjacent regions, the rainwater accumulates in that region. This also applies to rainwater flowing from other regions. The outer part of JOI Caldera is surrounded by steep cliffs of the outer ridge, so rainwater never flows out of JOI Caldera.

For a region, if it only rains in that region and eventually rainwater accumulates in multiple regions, that region is called a ridge. For the mountaineers who love the spectacular views, create a program to determine how many ridge regions there are.


Input Format

The input consists of $ 1 + H $ lines.

The first line contains two integers $ H, W $ ($1 \leq H \leq 1,000$, $1 \leq W \leq 1,000$), indicating that JOI Caldera spans $ H $ kilometers north-south and $ W $ kilometers east-west.

The following $ H $ lines each contain $ W $ integers separated by spaces, representing the elevation information. The $ i $-th line ($1 \leq i \leq H$) contains integers $ M_{i,j} $ ($1 \leq M_{i,j} \leq H \times W$), which represent the elevation of the region $ i $ kilometers from the north and $ j $ kilometers from the west. If $(i, j) \neq (k, l)$, then $M_{i,j} \neq M_{k,l}$.

Output Format

Output the number of ridge regions in one line.


Sample Input and Output

Sample Input #1

3 3
2 9 4
7 5 3
6 1 8

Sample Output #1

4

Sample Input #2

3 5
5 3 8 2 14
9 10 4 1 13
12 7 11 6 15

Sample Output #2

4

Notes/Hints

Sample Explanation 1

In Sample Input #1, the regions with elevations $ 5, 7, 8, 9 $ are ridges. For example, if it rains in the region with elevation $ 9 $, eventually the rainwater will accumulate in the regions with elevations $ 1, 2, 3 $. Therefore, the region with elevation $ 9 $ is a ridge. Also, if it rains in the region with elevation $ 6 $, eventually the rainwater will only accumulate in the region with elevation $ 1 $. Therefore, the region with elevation $ 6 $ is not a ridge.

Sample Explanation 2

In Sample Input #2, the regions with elevations $ 8, 10, 11, 12 $ are ridges.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 3 2 9 4 7 5 3 6 1 8
样例输出
4