Dynamic Grid (Small)

Count the edge-connected groups of 1s in a binary grid after each point update by flood fill.

Easy3BFSGraphMatrixInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

You have a grid with RR rows and CC columns, and every cell holds 0 or 1. You perform NN operations on the grid. Each operation is one of these two.

  • Operation M: change one cell of the grid to 0 or 1.
  • Operation Q: count the connected regions of 1s. A connected region of 1s is a maximal set of cells that all hold 1 such that you can walk from any cell of the set to any other cell of the set by moving only between cells that share an edge. Two cells that touch only at a corner are not connected.

Input

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

Each test case starts with a line holding two integers RR and CC, the number of rows and the number of columns. The next RR lines each hold a string of CC characters that are 0 or 1, and these lines are the initial grid. Rows are numbered from 0 at the top, columns from 0 at the left.

The next line holds one integer NN, the number of operations. The next NN lines each hold one operation. An M operation is written M x y z and sets the cell in row xx and column yy to zz. A Q operation is written Q.

Output

For each test case, first print one line holding Case #x:, where xx is the test case number starting from 1. Then, for every Q operation of that test case, in order, print one line holding the number of connected regions of 1s. A test case with no Q operation prints only its Case #x: line.

Limits

  • 1T101 \le T \le 10
  • 1R,C1001 \le R, C \le 100
  • 0x<R0 \le x < R
  • 0y<C0 \le y < C
  • 0z10 \le z \le 1
  • 1N101 \le N \le 10