Under construction

Still being built. Things may change

Ramps

Time limit2sMemory limit512 MB

Summary
Count rows or columns where every cell has equal height, or where unit ramps of length L can bridge each height step of exactly 1.
Level

Medium5 of 10

Topics
Implementation, Simulation, Array
Solved
No attempts yet

Problem

You are given an N×NN \times N map. Each cell holds the height of the ground there.

You want to count how many roads on this map can be crossed. A road is one whole row or one whole column, and crossing it means going from one end to the other end. There are 2N2N roads in total.

Look at this map with N=6N = 6.

Its 2N2N roads are drawn below.

A road can be crossed when every cell on it has the same height. Otherwise you can lay ramps to build a road you can cross. A ramp always has height 1 and length LL, and you never run out of ramps. A ramp connects a lower cell to a higher cell and must satisfy the following conditions.

  • A ramp is laid on the lower side, and its bottom must touch LL consecutive cells along their whole length.
  • The height difference between the lower cell and the higher cell must be 1.
  • The LL cells under a ramp must be consecutive and must all have the same height.

A ramp cannot be laid in the following cases.

  • Laying a ramp on a cell that another ramp already covers.
  • The height difference between the lower cell and the higher cell is not 1.
  • The cells on the lower side do not all have the same height, or LL of them are not consecutive.
  • The ramp runs outside the map.

The picture below shows where a ramp can be laid when L=2L = 2.

The next picture shows cases where a ramp cannot be laid.

Number those four cases 1 to 4 from the left. Case 1 fails because the height difference is not 1, case 2 because the bottom of the ramp does not touch the cells, case 3 because the ramps overlap, and case 4 because the ramp is laid at a slant.

For the N=6N = 6 map shown at the top with L=2L = 2, the roads that can be crossed are drawn in blue and the roads that cannot are drawn in red.

Given the map, write a program that counts the roads that can be crossed.

Input

The first line contains NN (2≤N≤1002 \le N \le 100) and LL (1≤L≤N1 \le L \le N). Each of the next NN lines contains NN numbers describing the map. The height of a cell is a natural number not greater than 10.

Output

Print the number of roads that can be crossed on the first line.

Examples4

  1. Example 1

    Input
    6 2
    3 3 3 3 3 3
    2 3 3 3 3 3
    2 2 2 3 2 3
    1 1 1 2 2 2
    1 1 1 3 3 1
    1 1 2 3 3 2
    
    Expected output
    3
    
  2. Example 2

    Input
    6 2
    3 2 1 1 2 3
    3 2 2 1 2 3
    3 2 2 2 3 3
    3 3 3 3 3 3
    3 3 3 3 2 2
    3 3 3 3 2 2
    
    Expected output
    7
    
  3. Example 3

    Input
    6 3
    3 2 1 1 2 3
    3 2 2 1 2 3
    3 2 2 2 3 3
    3 3 3 3 3 3
    3 3 3 3 2 2
    3 3 3 3 2 2
    
    Expected output
    3
    
  4. Example 4

    Input
    6 1
    3 2 1 1 2 3
    3 2 2 1 2 3
    3 2 2 2 3 3
    3 3 3 3 3 3
    3 3 3 3 2 2
    3 3 3 3 2 2
    
    Expected output
    11