P3456 [POI 2007] GRZ-Ridges and Valleys

Description

Byteasar loves trekking in the hills. During the hikes he explores all the ridges and valleys in vicinity. Therefore, in order to plan the journey and know how long it will last, he must know the number of ridgesand valleys in the area he is going to visit. And you are to help Byteasar. Byteasar has provided you with a map of the area of his very next expedition. The map is in the shape ofa $n\times n$ square. For each field $(i,j)$ belonging to the square(for $i,j\in \{1,\cdots,n\}$), its height $w_{(i,j)}$ is given. We say two fields are adjacent if they have a common side or a common vertex (i.e. the field $(i,j)$ is adjacent to the fields $(i-1,j-1)$,$(i-1,j)$,$(i-1,j+1)$,$(i,j-1)$,$(i,j+1)$,$(i+1,j-1)$,$(i+1,j)$,$(i+1,j+1)$, provided that these fields are on the map). We say a set of fields $S$ forms a ridge (valley) if: all the fields in $S$ have the same height,the set $S$ forms a connected part of the map (i.e. from any field in $S$ it is possible to reach any other field in $S$ while moving only between adjacent fields and without leaving the set $S$),if $s\in S$ and the field $s'\notin S$ is adjacent to $s$, then $w_s>w_{s'}$(for a ridge) or $w_s

Input Format

In the first line of the standard input there is one integer $n$ ($2\le n\le 1\ 000$)denoting the size of the map. Ineach of the following $n$ lines there is the description of the successive row of the map. In $(i+1)$'th line(for $i\in \{1,\cdots,n\}$) there are $n$ integers $w_{(i,1)},\cdots,w_{(i,n)}$($0\le w_i\le 1\ 000\ 000\ 000$), separated by single spaces. Thesedenote the heights of the successive fields of the $i$'th row of the map.

Output Format

The first and only line of the standard output should contain two integers separated by a single space -thenumber of ridges followed by the number of valleys for the landscape described by the map.