12729 - Cocoa the Abstract Artist   

Description

Problem brought to you by NTHU Order a Rabbit Club.

A reliable oneesan, a genius in arithmetics, an amazing baker… that is our protagonist - Hoto Cocoa. The young girl has many talents for sure, but art seems to be not one of them.

As much as Cocoa wants to appear reliable, she is so depressed how bad her art homework turns out, that she asks Chino to help her improve her drawing.

Cocoa’s drawing can be represented as a 2D array of size N*M: N rows with M integers in each row. Each integer represents the color on that position; two positions have the same color iff they have the same integers, to be more precise.

Chino is doubtlessly great at art, but only abstract art. She will view a piece of drawing acceptable, if and only if each color that appears in the drawing forms a single connected component.

That is, for any two blocks that share the same color ci, one can find a way to walk from one to the other using only 4 directions: up, down, left, right, while only walking on blocks of color ci.

For example, in the drawing below, there are two colors 0 and 6 that fail to form a single connected component.

 

If Cocoa’s drawing fails this criteria for a good abstract art, Chino wants to point out what colors violate the rules, and should be eliminated in order for the drawing to be acceptable. But since Cocoa has made a very big painting, Chino wants you to write a program to help her find these colors.

(If you help Chino maybe she’ll let you pat her head!)

Input

Each input file contains only one testcase. For each test case, there will be one line and a 2D array A that describes Cocoa’s drawing.

The first line contains a pair of integers N M, denoting the size of the drawing. Both N and M will not exceed 1000.

Hence come N lines, each having M integers. The j-th integer in the i-th line, Aij, has the range 0 to 65535.

 

Output

For each testcase, if the drawing is an acceptable abstract art (by Chino’s standards), print a single word “ABSTRACT”(without quotation marks); else, if there are k colors that need to be eliminated, print the numbers of these colors on k separate lines in ascending order.

Sample Input  Download

Sample Output  Download

Tags




Discuss