QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#3498. Modern Art

Statistics

Art critics worldwide have only recently begun to recognize the creative genius behind the great bovine painter, Picowso.

Picowso paints in a very particular way. She starts with an $N \times N$ blank canvas, represented by an $N \times N$ grid of zeros, where a zero indicates an empty cell of the canvas. She then draws $N^2$ rectangles on the canvas, one in each of $N^2$ colors (conveniently numbered $1 \ldots N^2$). For example, she might start by painting a rectangle in color 2, giving this intermediate canvas:

2 2 2 0 
2 2 2 0 
2 2 2 0 
0 0 0 0

She might then paint a rectangle in color 7:

2 2 2 0 
2 7 7 7 
2 7 7 7 
0 0 0 0

And then she might paint a small rectangle in color 3:

2 2 3 0 
2 7 3 7 
2 7 7 7 
0 0 0 0

Each rectangle has sides parallel to the edges of the canvas, and a rectangle could be as large as the entire canvas or as small as a single cell. Each color from $1 \ldots N^2$ is used exactly once, although later colors might completely cover up some of the earlier colors.

Given the final state of the canvas, please count how many of the $N^2$ colors could have possibly been the first to be painted.

Input Format

The first line of input contains $N$, the size of the canvas ($1 \leq N \leq 1000$). The next $N$ lines describe the final picture of the canvas, each containing $N$ integers that are in the range $0 \ldots N^2$. The input is guaranteed to have been drawn as described above, by painting successive rectangles in different colors.

Output Format

Please output a count of the number of colors that could have been drawn first.

Sample Input

4
2 2 3 0
2 7 3 7
2 7 7 7
0 0 0 0

Sample Output

14

In this example, color 2 could have been the first to be painted. Color 3 clearly had to have been painted after color 7, and color 7 clearly had to have been painted after color 2. Since we don't see the other colors, we deduce that they also could have been painted first.

Problem credits: Brian Dean

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.