Dynamic Grid (Large)

Point updates flip binary grid cells and each query asks for the number of edge-connected groups of 1s.

Medium4BFSMatrixSimulationInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

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

  • Operation M: change the value of one cell of the grid to 0 or 1.
  • Operation Q: report the number of connected regions of 1s. A connected region of 1s is a set of cells that all hold 1, and from any cell of the set you reach every other cell of the set by moving 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. Each test case starts with a line holding two integers RR and CC, the number of rows and the number of columns of the grid. The next RR lines each hold a string of length CC made only of the characters 0 and 1, giving the initial state of the grid. Rows are numbered from 00 to R1R-1 and columns from 00 to C1C-1.

The next line holds one integer NN, the number of operations to perform. NN lines follow, one operation per line. An operation M has the form M x y z and means that the cell at row xx and column yy takes the value zz. An operation Q is the single character Q.

Output

For each test case, first print Case #x: on its own line, where xx is the test case number starting from 1. Then, for every operation Q of that test case, in the order the operations are given, print the number of connected regions of 1s on its own line.

Constraints

  • 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
  • 1N10001 \le N \le 1000