Ridges and Valleys

No attempts yetTime limit3sMemory limit128 MB

Problem

Byteasar loves trekking in the hills. During his hikes he explores every ridge and valley nearby. To plan a trip and know how long it will take, he needs to know how many ridges and how many valleys lie in the area he is about to visit. Your job is to help him.

Byteasar gives you a map of his next expedition. The map is an n×nn \times n square. For every field (i,j)(i, j) of the square (with i,j{1,,n}i, j \in \{1, \dots, n\}) its height w(i,j)w(i, j) is given.

Two fields are adjacent when they share a side or a corner. In other words, field (i,j)(i, j) is adjacent to (i1,j1)(i-1, j-1), (i1,j)(i-1, j), (i1,j+1)(i-1, j+1), (i,j1)(i, j-1), (i,j+1)(i, j+1), (i+1,j1)(i+1, j-1), (i+1,j)(i+1, j) and (i+1,j+1)(i+1, j+1), whenever those fields still lie on the map.

A set of fields SS forms a ridge (respectively a valley) when:

  • all fields in SS have the same height,
  • SS is connected, that is, from any field in SS you can reach any other field in SS by stepping only between adjacent fields without ever leaving SS,
  • for every field sSs \in S and every field sSs' \notin S adjacent to ss, the heights satisfy ws>wsw_s > w_{s'} (for a ridge) or ws<wsw_s < w_{s'} (for a valley).

In particular, if every field on the map has the same height, all of them together form both a ridge and a valley.

Determine the number of ridges and the number of valleys in the landscape described by the map.

Input

The first line contains one integer nn (2n10002 \le n \le 1000), the size of the map. Each of the next nn lines describes one row of the map: line i+1i + 1 (for i{1,,n}i \in \{1, \dots, n\}) contains nn integers w(i,1),,w(i,n)w(i, 1), \dots, w(i, n) (0w(i,j)1090 \le w(i, j) \le 10^9), separated by single spaces, the heights of the fields in row ii.

Output

Print a single line with two integers separated by one space: the number of ridges followed by the number of valleys in the landscape described by the map.

Hint

In the figures above the ridges are drawn with a solid line and the valleys with a dashed line.