5019: Igor In the Museum 伊戈尔在博物馆

内存限制:256 MB 时间限制:2 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:3 通过:3

题目描述

伊戈尔在博物馆里,他想看到尽可能多的照片。

博物馆可以表示为n×m个单元格的矩形区域。每个单元格要么是空的,要么是无法通行的。空单元格标有“.”,不可通行的单元格标有“*”。每隔两个不同类型的相邻牢房(一个空的,一个无法通行的)被一堵包含一幅图片的墙隔开。

一开始,伊戈尔在某个空牢房里。在任何时候,他都可以移动到任何与当前单元格共享一侧的空单元格。

对于几个起始位置,您应该计算 Igor 可以看到的最大图片数量。伊戈尔只有在有这张照片的墙附近的牢房里时才能看到这张照片。伊戈尔有很多时间,所以他会检查他能看到的每一张照片。

Igor is in the museum and he wants to see as many pictures as possible.
Museum can be represented as a rectangular field of n×m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.
At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.
For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.
Input
First line of the input contains three integers n, m and k (3≤n,m≤1000,1≤kmin(n·m,100000))− the museum dimensions and the number of starting positions to process.
Each of the next n lines contains m symbols '.', '*' − the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.
Each of the last k lines contains two integers x and y (1≤xn,1≤ym)− the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns − from left to right. It is guaranteed that all starting positions are empty cells.
Output
Print k integers− the maximum number of pictures, that Igor can see if he starts in corresponding position.
Examples
Input
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
Output
6
4
10
Input
4 4 1
****
*..*
*.**
****
3 2
Output
8 

输入格式

输入的第一行包含三个整数 n、m 和 k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min( n·m, 100 000)) — 博物馆尺寸和要处理的起始位置数量。

接下来的n行中的每一行都包含m符号“.”,“*” — 博物馆的描述。保证所有边界牢房都无法通行,所以伊戈尔不能从博物馆出去。

最后 k 行中的每一行都包含两个整数 x 和 y(1 ≤ x ≤ n,1 ≤ y ≤ m)——分别是伊戈尔起始位置之一的行和列。行从上到下编号,列 — 从左到右编号。保证所有起始位置都是空单元格。

输出格式

打印 k 个整数 — 伊戈尔可以看到的最大图片数量,如果他从相应的位置开始。

输入样例 复制

5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3

输出样例 复制

6
4
10