Paint by Letters
Bessie has recently received a painting set. The canvas can be represented as an n * m rectangle of cells where the rows are labeled 1..n from top to bottom and the columns are labeled 1..m from left to right. Once painted, the color of a cell can be represented by an uppercase letter from 'A' to 'Z'. Initially, all cells are uncolored, and a cell cannot be painted more than once.
Bessie has specified the color that she desires for each cell. She can paint a set of cells with a single color in one stroke if the set forms a connected component, meaning that any cell in the set can reach any other via a sequence of adjacent cells. Two cells are considered to be adjacent if they share an edge.
For example, the 3 * 3 canvas
AAB BBA BBB
can be colored in four strokes as follows:
... ..B AAB AAB AAB ... -> ... -> ... -> BB. -> BBA ... ... ... BBB BBB
It is not possible to produce the end result using less than four strokes.
Being an avant-garde artist, Bessie will end up painting only a subrectangle of the canvas. Currently, she is considering q candidates, each of which can be represented by four integers x[1]
, y[1]
, x[2]
and y[2]
. This means that the subrectangle consists of all cells with row in the range x[1]
to x[2]
inclusive and column in the range y[1]
to y[2]
inclusive.
For each candidate subrectangle, what is the minimum number of strokes needed to paint each cell in the subrectangle with its desired color while leaving all cells outside the subrectangle uncolored? Note that Bessie does not actually do any painting during this process, so the answers for each candidate are independent.
Input
The first line contains n, m (1 ≤ n, m ≤ 1000) and q (1 ≤ q ≤ 1000).
The next n lines each contain a string of m uppercase characters representing the desired colors for each row of the canvas.
The next q lines each contain four integers x[1]
, y[1]
, x[2]
, y[2]
representing a candidate subrectangle (1 ≤ x[1]
≤ x[2]
≤ n, 1 ≤ y[1]
≤ y[2]
≤ m).
Output
For each of the q candidates, output the answer on a new line.
Example
The first candidate consists of the entire canvas, which can be painted in six strokes.
The second candidate consists of the subrectangle with desired colors
ABBA
and can be colored in three strokes. Note that although the cells at (3, 5) and (3, 8) can be colored with A in a single stroke if you consider the entire canvas, this is not the case when considering only the cells within the subrectangle.