Colorful Quadrants
Time limit1sMemory limit2048 MB
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 grid. Some of its points are colored with one of colors. The color of a point is an integer from to , where means the point is uncolored. Several points may share the same color. Rows and columns are numbered from to , and the point in row and column is written .
For an uncolored point with and , remove row and column from the grid. This leaves four sub-grids, called NW (northwest), NE (northeast), SW (southwest), and SE (southeast) according to their position relative to . The point 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 grid. The point has colorful quadrants: NW has color , NE has color , SW has color , and SE has color (Figure C.1(b)). The point does not, because SW and SE both contain only color (Figure C.1(c)).
Given an 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 and (, ), where is the number of rows and columns of the grid and is the number of colors. Each of the next lines contains integers. The -th integer on the -th line is the color of point , where .
Output
Print one line containing the number of uncolored points that have colorful quadrants.