Under construction

Still being built. Things may change

Lights

Interview

Time limit1sMemory limit128 MB

Summary
You press switches that flip the rectangle from the origin to the switch and need the fewest presses to turn every bulb on.
Level

Medium5 of 10

Topics
Greedy, Prefix sum
Solved
No attempts yet

Problem

A rectangular panel has RR rows with CC bulbs in each row. The rows are numbered from 0 to R−1R-1 and the columns from 0 to C−1C-1. Some of the bulbs are on and the rest are off.

The panel is wired to a switch board. The switches sit in a rectangle of RR rows and CC columns, the same size as the panel.

Pressing one switch flips the state of some bulbs. Bulbs that were on turn off, and bulbs that were off turn on. More precisely, pressing the switch at (row,col)(\text{row}, \text{col}) changes the state of every bulb at (x,y)(x, y) with x≤rowx \le \text{row} and y≤coly \le \text{col}. In other words, it flips every bulb inside the rectangle whose opposite corners are (0,0)(0, 0) and (row,col)(\text{row}, \text{col}).

You are given a matrix with the initial state of the bulbs. The character 1 is a bulb that is on, and the character 0 is a bulb that is off.

Find the minimum number of presses needed to have every bulb on at the same time.

Input

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

Each test case starts with a line holding two integers RR and CC. The next RR lines hold CC characters each, and this R×CR \times C matrix is the initial state of the bulbs. RR and CC are between 1 and 500 inclusive.

Output

For each test case, print one line in the format Case #x: M. Here xx is the case number starting from 1, and MM is the minimum number of presses needed to have every bulb on at the same time.

Examples1

  1. Example 1

    Input
    6
    3 7
    0001111
    0001111
    1111111
    3 7
    0000000
    0000000
    0000000
    3 7
    1111111
    1111111
    1111111
    1 5
    01001
    1 10
    1010101010
    4 4
    0101
    1010
    0101
    1010
    
    Expected output
    Case #1: 1
    Case #2: 1
    Case #3: 0
    Case #4: 3
    Case #5: 10
    Case #6: 7