#K39342. Flood Fill Queries

    ID: 26399 Type: Default 1000ms 256MiB

Flood Fill Queries

Flood Fill Queries

You are given a grid of size \(R \times C\) where each cell contains an integer representing its color. You need to process \(Q\) queries on this grid. In each query, you are given a starting cell \((x, y)\) and a new color. Using the flood fill algorithm, you must change the color of the starting cell and all of its connected cells that have the same original color to the new color.

Two cells are considered connected if they share an edge. Mathematically, cells \((i_1, j_1)\) and \((i_2, j_2)\) are connected if and only if \(|i_1-i_2| + |j_1-j_2| = 1\) and they have the same color.

After each query, output the number of cells that were changed. Note that the grid is modified after each query and subsequent queries operate on the updated grid.

inputFormat

The first line contains three integers \(R, C, Q\) representing the number of rows, columns, and queries respectively.

The next \(R\) lines each contain \(C\) space-separated integers, representing the initial colors of the grid cells.

The following \(Q\) lines each contain three space-separated integers \(x, y, new_color\), where \(x\) and \(y\) denote the 0-indexed coordinates of the starting cell and \(new_color\) is the color to which the component should be changed.

outputFormat

For each query, output a single integer on a new line representing the number of cells that were changed by that query.

## sample
1 1 1
0
0 0 1
1

</p>