This page is still under construction.

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

Crazy Rows (Large)

Time limit5sMemory limit512 MB

Summary
Given a binary N x N matrix, swap adjacent rows to move every 1 to or below the main diagonal, and output the minimum number of swaps.
Level

Medium5 of 10

Topics
Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

You are given an N×NN \times N matrix whose entries are 0 and 1. You may swap any two adjacent rows.

The goal is to place every 1 on or below the main diagonal. That is, for every XX with 1≤X≤N1 \le X \le N, row XX must contain no 1 to the right of column XX.

Find the minimum number of row swaps needed to reach the goal.

Input

The first line contains the number of test cases TT. TT test cases follow.

The first line of each test case contains one integer NN. Each of the next NN lines contains NN characters. Each character is 0 or 1.

Limits

  • 1≤T≤601 \le T \le 60
  • 1≤N≤401 \le N \le 40

Output

For each test case, print one line in this format.

Case #X: K

Here XX is the test case number starting from 1, and KK is the minimum number of row swaps needed to place every 1 on or below the main diagonal.

Every test case is guaranteed to have a solution.

Examples2

  1. Example 1

    Input
    3
    2
    10
    11
    3
    001
    100
    010
    4
    1110
    1100
    1100
    1000
    
    Expected output
    Case #1: 0
    Case #2: 2
    Case #3: 4
    
  2. Example 2

    Input
    3
    2
    11
    10
    3
    111
    100
    010
    1
    1
    
    Expected output
    Case #1: 1
    Case #2: 2
    Case #3: 0