QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#12690. Ridges and Valleys

الإحصائيات

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 ridges and 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 of a $n \times n$ square. For each field $(i,j)$ belonging to the square (for $i,j \in \{1,…,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 < w_{s'}$ (for a valley).

In particular, if all the fields on the map have the same height, they form both a ridge and a valley.

Your task is to determine the number of ridges and valleys for the landscape described by the map.

Write a programme that:

  • reads from the standard input the description of the map,
  • determines the number of ridges and valleys for the landscape described by this map,
  • writes out the outcome to the standard output.

Input

In the first line of the standard input there is one integer $n$ ($2 ≤ n ≤ 1\,000$) denoting the size of the map. In each 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,…,n\}$) there are $n$ integers $w(i,1)$, ..., $w(i,n)$ ($0 ≤ w_i ≤ 1\,000\,000\,000$), separated by single spaces. These denote the heights of the successive fields of the $i$'th row of the map.

Output

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

Example

Input

5
8 8 8 7 7
7 7 8 8 7
7 7 7 7 7
7 8 8 7 8
7 8 8 8 8

Output

2 1
problem_12690_1.gif

Input 2

5
5 7 8 3 1
5 5 7 6 6
6 6 6 2 8
5 7 2 5 8
7 1 0 1 7

Output 2

3 3
problem_12690_2.gif

The above figures show the ridges (continuous line) and the valleys (dashed line).

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.