This page is still under construction.

Parts of this page are still being built. What you see may change.

Contest Venue Setup

Interview

Time limit1sMemory limit128 MB

Summary
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 NN rows and MM 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 TT. (1≤T≤1001 \le T \le 100)

The first line of each test case contains the number of rows NN and the number of columns MM of the hall. (1≤N,M≤1001 \le N, M \le 100)

Each of the next NN lines contains MM integers. The jj-th number on the ii-th line is the school number of the team seated in row ii, column jj, and it is between 1 and 100. If the number is −1-1, 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.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    2
    0
    1