Treasure Hunting

Time limit: 1.15s Memory limit: 512MB Input: Output:

After he beat Tanaka in last year’s cook-off contest (yes, the most important contest in all of Info(1)Cup Kingdom), Lulu decided to quit cooking and go treasure hunting. But since Tanaka is a very ambitious person, he wants to get his revenge and beat Lulu this time. The hunt takes place inside of a maze which can be represented as a matrix of size n×mn \times m. Each cell (x,y)(x, y) can be either a wall (represented by the character #) or a treasure cell (represented by the character $). Each treasure cell can contain at most one treasure. Initially, all treasure cells contain a treasure.

We say that the cell (x,y)(x', y') is reachable from (x,y)(x, y) if one can get from (x,y)(x, y) to (x,y)(x', y') by moving only down or to the right through treasure cells. Note that a treasure cell is reachable from itself.

The maze has a very interesting property. The cell (n,m)(n, m) is reachable from any treasure cell and any treasure cell is reachable from (1,1)(1, 1). Let F(x,y)F(x, y) be the number of treasure cells which contain a treasure and can be reached from (x,y)(x, y). We define F(x,y)=0F(x, y) = 0 if (x,y)(x, y) is a wall. Tanaka thinks that he could get ahead of his opponent by finding SS, the sum of F(x,y)F(x, y) for all 1xn1 ≤ x ≤ n and 1ym1 ≤ y ≤ m, i.e.
S=x=1ny=1mF(x,y) \displaystyle S = \sum_{x=1}^{n} \sum_{y=1}^{m} F(x, y)

But then the real treasure hunt begins! At each moment, one of two things can happen:

  1. The cell (x,y)(x, y) gets an update. If the cell previously had a treasure in it, then the treasure disappears. Otherwise, a treasure appears in the cell (x,y)(x, y).
  2. Tanaka wants to know F(x,y)F(x, y) for a given cell (x,y)(x, y).

Tanaka doesn’t have enough time to do all this on his own, so he needs your programming skills. Help Tanaka beat Lulu by writing a program which calculates the value SS, then answers all of his queries right.

Input data

The first line of input contains the integers n,mn, m and QQ, the number of rows and columns in the matrix and the number of operations you need to process. The next nn lines contain mm characters, representing the maze. Each of the next QQ lines contains an operation, which will be represented as follows:

  • ! x y, which means the cell (x,y)(x, y) gets an update.
  • ? x y, which means you have to output the value F(x,y)F(x, y).

It is guaranteed that (x,y)(x, y) is a treasure cell in both cases.

Output data

The first line of output must contain the value SS, the sum of F(x,y)F(x, y) for all cells (x,y)(x, y) in the initial state. Each of the next lines must contain the answers to Tanaka’s queries, in order.

Restrictions

  • 1n,m1 0001 ≤ n, m ≤ 1 \ 000
  • 1Q50 0001 ≤ Q ≤ 50 \ 000
  • It is guaranteed that both (1,1)(1, 1) and (n,m)(n, m) are treasure cells
  • 50% of the points for each subtask are awarded for finding SS, and the other 50% for answering the queries. Please note that you still have to output the value SS, even if it is not correct, in order to get the points for the queries.

Subtask 1 (5 points)

  • n=1n = 1 or m=1m = 1

Subtask 2 (7 points)

  • All cells (x,y)(x, y) are treasure cells

Subtask 3 (8 points)

  • The cell (x,y)(x, y) is a wall for all 2xn12 ≤ x ≤ n − 1 and 2ym12 ≤ y ≤ m − 1

Subtask 4 (12 points)

  • n,m50n, m ≤ 50

Subtask 5 (18 points)

  • Q50Q ≤ 50

Subtask 6 (27 points)

  • n,m240n, m ≤ 240

Subtask 7 (23 points)

  • No further restrictions

Examples

stdin

5 5 5
$$$$$
$$$#$
$#$$$
$$$#$
$$$$$
! 5 4
? 2 2
! 4 5
? 5 5
? 3 4

stdout

159
9
1
3

Explanations

First example In the initial state, the maze looks like this:

The values F(x,y)F(x, y) for the whole maze are:

For the first query, the maze looks like this:

The red cell is the starting cell and the blue cells are those reachable from it. Cells that contain a treasure are marked with a $ sign, and cells without are empty. The number of such cells which contain a treasure is 99.
For the second query, the maze looks like this:

The only cell reachable from (5,5)(5, 5) is (5,5)(5, 5).
For the third query, the maze looks like this:

The red cell is the starting cell and the blue cells are those reachable from it. The number of such cells which contain a treasure is 33.

Log in or sign up to be able to send submissions!