This page is still under construction.

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

Irrigation Lines

Time limit1sMemory limit256 MB

Summary
Open the fewest row and column lines so every planted cell shares a row or column with an open line.
Level

Medium6 of 10

Topics
Graph, DFS, BFS
Solved
No attempts yet

Problem

A plantation is made of rectangular fields, and each field is divided into square zones. Crops are rotated, so in one season some zones are planted and the others lie fallow.

Every field has its own irrigation system. The main water line runs around the field, and valves connect it to the lateral irrigation lines. There is one line for each row of zones and one line for each column, so a field with MM rows and NN columns has M+NM + N lines. Water flows through a line while its valve is open and stops when the valve is closed. A planted zone is watered as long as the line of its row or the line of its column is open. On an open line, the emitters over the zones that need no water are closed.

To keep the management effort low, the system has to open as few lines as possible. Read the layout of a field and compute the smallest number of irrigation lines that must be open to water every planted zone.

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100).

Each test case starts with a line holding the number of rows MM and the number of columns NN (1≤M,N≤1001 \le M, N \le 100), separated by a space. The next MM lines each contain a string of NN characters, where 1 marks a planted zone and 0 marks a fallow zone.

Output

For each test case, print one line in the form "Case #X: Y", where XX is the test case number starting from 1 and YY is the smallest number of irrigation lines that must be open. Put one space after the colon.

Examples6

  1. Example 1

    Input
    2
    4 4
    0010
    0101
    0010
    0000
    5 4
    1001
    0010
    1100
    1110
    0101
    
    Expected output
    Case #1: 2
    Case #2: 4
    
  2. Example 2

    Input
    3
    1 1
    0
    1 1
    1
    3 3
    000
    000
    000
    
    Expected output
    Case #1: 0
    Case #2: 1
    Case #3: 0
    
  3. Example 3

    Input
    2
    4 7
    1111111
    1111111
    1111111
    1111111
    7 4
    1111
    1111
    1111
    1111
    1111
    1111
    1111
    
    Expected output
    Case #1: 4
    Case #2: 4
    
  4. Example 4

    Input
    1
    6 6
    100000
    010000
    001000
    000100
    000010
    000001
    
    Expected output
    Case #1: 6
    
  5. Example 5

    Input
    2
    1 100
    1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    100 1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    
    Expected output
    Case #1: 1
    Case #2: 1
    
  6. Example 6

    Input
    1
    5 6
    000100
    000100
    111111
    000100
    010100
    
    Expected output
    Case #1: 3