Under construction

Still being built. Things may change

Colorful Quadrants

Time limit1sMemory limit2048 MB

Summary
Count the uncolored interior points where each of the four quadrants can supply one point, with all four chosen colors different.
Level

Medium7 of 10

Topics
Prefix sum, Bit manipulation, Matrix
Solved
No attempts yet

Problem

You are given an n×nn \times n grid. Some of its points are colored with one of kk colors. The color of a point is an integer from 00 to kk, where 00 means the point is uncolored. Several points may share the same color. Rows and columns are numbered from 11 to nn, and the point in row ii and column jj is written (i,j)(i,j).

For an uncolored point (i,j)(i,j) with 1<i<n1 < i < n and 1<j<n1 < j < n, remove row ii and column jj from the grid. This leaves four sub-grids, called NW (northwest), NE (northeast), SW (southwest), and SE (southeast) according to their position relative to (i,j)(i,j). The point (i,j)(i,j) has colorful quadrants if you can choose one point from each of the four sub-grids so that the four chosen points all have different colors.

Figure C.1

See Figure C.1(a) for a 5×55 \times 5 grid. The point (2,3)(2,3) has colorful quadrants: NW has color 11, NE has color 44, SW has color 33, and SE has color 22 (Figure C.1(b)). The point (4,3)(4,3) does not, because SW and SE both contain only color 22 (Figure C.1(c)).

Given an n×nn \times n grid that contains at least four colored points with different colors, count the number of uncolored points that have colorful quadrants.

Input

The first line contains two integers nn and kk (3≤n≤2 0003 \le n \le 2\,000, 4≤k≤1 0004 \le k \le 1\,000), where nn is the number of rows and columns of the grid and kk is the number of colors. Each of the next nn lines contains nn integers. The jj-th integer on the ii-th line is the color cc of point (i,j)(i,j), where 0≤c≤k0 \le c \le k.

Output

Print one line containing the number of uncolored points that have colorful quadrants.

Examples3

  1. Example 1

    Input
    5 4
    0 1 0 0 4
    2 0 0 1 3
    3 0 2 0 0
    0 0 0 0 0
    0 2 1 2 0
    
    Expected output
    1
    
  2. Example 2

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

    Input
    4 8
    0 1 2 0
    8 0 0 3
    7 0 0 4
    0 6 5 0
    
    Expected output
    0