eolymp
bolt
Try our new interface for solving problems
Problems

Contest Hall Preparation

Contest Hall Preparation

Before the ACM-ACPC regional contest, the site director and the volunteers were very busy preparing for the contest. One of their tasks is to assign a table for each team such that no two teams from the same university are adjacent to each other. The site director decided not to waste his time doing this task and asked the judges to do it. The judges thought this could be a good problem to be used in the contest problems set. As they were very busy preparing for the contest, the judges decided to solve part of the problem and ask the contestants to solve the rest. The judges will generate a number of layouts for the teams assignment to the tables and will ask the contestants to write a program to check whether each of these layouts is valid or not. If a layout is not valid the program should count how many dierent universities have at least two of their teams sitting adjacent to each other. Well, you may use those solutions for the next year's contest, said by the chief judge Ahmed Aly to the site director. The contest hall can be represented as a \textbf{2D} grid of \textbf{N} rows with \textbf{M} cells in each row. Each cell in the grid is either occupied by a team or empty. There could be up to \textbf{8} teams adjacent to a single team. A team may have less than \textbf{8 }adjacent teams if it is seated next to a hall edge or some of its adjacent cells are empty. For example, in the layout shown in the following gure, team \textbf{E} has \textbf{7} adjacent teams, named \textbf{A}, \textbf{B}, \textbf{C}, \textbf{D}, \textbf{F}, \textbf{G} and \textbf{H}, while the adjacent teams to team \textbf{A} are \textbf{B}, \textbf{D} and \textbf{E}. \includegraphics{https://static.e-olymp.com/content/7d/7d5318ab1ad4bb7fa72b9d3dbfdef7572a6e7cf8.jpg} \InputFile Your program will be tested on one or more test cases. The first line of the input will be a single integer \textbf{T}, the number of test cases (\textbf{1} ≤ \textbf{T} ≤ \textbf{100}). Followed by the test cases, each one starts with a line containing \textbf{2} integers separated by a single space \textbf{N} and \textbf{M} (\textbf{1} ≤ \textbf{N}, \textbf{M} ≤ \textbf{100}) representing the dimensions of the hall, followed by \textbf{N} lines each line contains \textbf{M} integers separated by a single space, representing the tables assignment in this row. Each integer represents the university \textbf{ID} of the team assigned to this table or \textbf{-1} if it is empty. All universities \textbf{ID}s are positive integers not greater than \textbf{100}. \OutputFile For each test case, print on a single line one integer, the number of dierent universities having at least two of their teams adjacent to each other.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
3 3
1 2 3
2 2 2
1 1 1
3 3
1 2 3
3 -1 1
2 -1 2
3 3
1 2 3
-1 1 5
1 2 4
Output example #1
2
0
1
Source Arab Collegiate Programming Contest 2012