Educational codeforces Round 1D "Dfs seeking Unicom block"

Source: Internet
Author: User

Http://blog.csdn.net/snowy_smile/article/details/49924965D. Igor in the Museumtime limit per test 1 secondmemory limit per test, Megabytesinput standard InputOutput standard OUTPU T

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 x m cells. Each cell is either empty or impassable. Empty cells is marked with '. ', impassable cells is marked with '* '. Every adjacent cells of different types (one empty and one impassable) is divided by a wall containing one picture.

At the beginning Igor are in some empty cell. At every moment he can move to any empty cell, which 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 of the only if he was in the cell adjacent to the wall with the this picture. Igor has a lot of time, so he'll examine every picture he can see.

Input

First line of the input contains three integers n, m and k (3≤ n, m ≤ 1000, 1≤ k ≤ min( n· M)-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 this all border cells be impassable, so Igor can ' t go out from the museum.

Each of the last K lines contains, integers x and y (1≤ x ≤ n, 1≤ y ≤ m)-the row and the column of one of Igor ' s starting positions respectively. Rows is numbered from the top to the bottom, Columns-from left to right. It is guaranteed, all starting positions is empty cells.

Output

Print k integers-the maximum number of pictures, that Igor can see if he starts in corresponding position.

Sample Test (s) input
5 6 3*******. *.********....*******2 22 54 3
Output
6410
Input
4 4 1*****. **.******3 2
Output
8

Educational codeforces Round 1D "Dfs seeking Unicom block"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.