문제
2 seconds, 128 MB, 140 points It is often heard that the apple doesn’t fall far from the tree. But is that really so? The National Statistics Department has tracked the falling of apples in a fruit garden for G consecutive years. The fruit garden can be represented as a matrix with dimensions R · S. Each field of the matrix can contain more than one apple tree. Interestingly enough, each year there was exactly one apple fall, so the Department decided to write down G pairs of numbers (ri, si) that denote the row and column of the location where the apple fell during the ith year. Moreover, by next year, a new tree grew at that location. Your task is to determine the squared distance between the nearest tree and the apple that fell, measured in unit fields of the matrix (we assume it is that tree from which the apple fell). The distance between fields (r1, s1) and (r2, s2) in the matrix are calculated as: d((r1, s1), (r2, s2)) = p (r1 −r2)2 + (s1 −s2)2
입력
The first line of input contains two integers, R and S (1 ⩽R, S ⩽500), the number of rows and columns of the matrix. Each of the following R lines contains S characters ’x’ or ’.’. The character ’.’ denotes an empty field, and the character ’x’ denotes a field with at least one tree. The fruit garden will initially contain at least one tree. After that, an integer G (1 ⩽G ⩽105) follows, the number of years the fruit garden has been under observation. Each of the following G lines describes the falls of the apples. Each line contains a pair of integers (ri, si) that denote the row and column of the location where the apple fell in the ith year.
출력
Output G numbers, the required squared distances from the task, each in its own line.
예제 입력 1
3 3
x..
...
...
3
1 3
1 1
3 2
예제 출력 1
4
0
5
예제 입력 2
5 5
..x..
....x
.....
.....
.....
4
3 1
5 3
4 5
3 5
예제 출력 2
8
8
4
1
코드를 제출하려면 로그인이 필요합니다.
로그인