Under construction

Still being built. Things may change

Roadside optimization

Interview

Time limit1sMemory limit256 MB

Summary
Given a graph as an adjacency matrix of reachability, find the minimum number of roads to keep so that all reachability relations stay the same.
Level

Medium4 of 10

Topics
Graph, Union-find, Implementation, Math
Solved
No attempts yet

Problem

Some towns in the province GG are connected by roads, and roads always run both ways. The budget for maintaining the road network is scarce, so it was decided to keep only the minimum number of roads. However, if it is currently possible to reach town BB from town AA, this must remain possible even after the reduction.

Determine the minimum number of roads that must be kept.

Input

The first line of the input file contains a single integer TT, the number of tests (1≤T≤50 0001 \le T \le 50\,000). The descriptions of TT tests follow.

The first line of test tt contains a single integer NtN_t, the number of towns (1≤Nt≤2001 \le N_t \le 200).

The next NtN_t lines contain NtN_t integers each, and every integer is either 00 or 11. If the jjth position of line ii is 00, then town jj cannot be reached from town ii (not even by passing through other towns); if it is 11, then there is a passage. A passage from a town to itself is assumed, so the iith position of line ii is always 11 (1≤i≤Nt1 \le i \le N_t).

The sum of Nt2N_t^2 over all tests is guaranteed not to exceed 50 00050\,000.

Output

For each test, print the minimum number of roads to be kept on a separate line.

Examples2

  1. Example 1

    Input
    1
    1
    1
    
    Expected output
    0
    
  2. Example 2

    Input
    2
    4
    1 1 1 1
    1 1 1 1
    1 1 1 1
    1 1 1 1
    7
    1 1 1 0 0 0 0
    1 1 1 0 0 0 0
    1 1 1 0 0 0 0
    0 0 0 1 1 0 1
    0 0 0 1 1 0 1
    0 0 0 0 0 1 0
    0 0 0 1 1 0 1
    
    Expected output
    3
    4