Contest Venue Setup
InterviewTime limit1sMemory limit128 MB
Count the schools that have two of their teams seated in king-adjacent cells of an N by M grid with empty seats.
- Level
Easy2 of 10
- Topics
- Matrix, Implementation
- Solved
- No attempts yet
Problem
Before a regional programming contest opens, the site officials and the volunteers are very busy. One of their jobs is arranging the tables in the hall so that two adjacent tables never seat teams from the same school.
Doing that by hand is tedious, so a program usually does it. This year the judges took the job over. Once the arrangement was done, the judges decided the seating problem was worth using as a contest problem, so here is part of it.
The judges' program is simple. It first assigns the seats arbitrarily. Then it checks whether any two adjacent seats hold teams from the same school. If such seats exist, it counts how many schools gain from the arrangement, that is, how many schools have two of their own teams in adjacent seats.
The hall is a table with rows and columns. Each cell seats one team or nobody. A team has at most 8 adjacent teams. A seat on the border of the table, or a seat with empty cells around it, has fewer than 8 adjacent teams.
For example, in the arrangement below a dot is an empty seat.
A B C
D E F
G H .
Team E is adjacent to A, B, C, D, F, G, H, so it has 7 adjacent teams. Team A is adjacent to B, D, E, so it has 3.
Input
The first line contains the number of test cases . ()
The first line of each test case contains the number of rows and the number of columns of the hall. ()
Each of the next lines contains integers. The -th number on the -th line is the school number of the team seated in row , column , and it is between 1 and 100. If the number is , that seat is empty.
Output
For each test case, print on one line how many schools have two of their own teams in adjacent seats.