This page is still under construction.

Parts of this page are still being built. What you see may change.

Looping Labyrinth

Time limit4sMemory limit512 MB

Summary
Decide for each of up to 200000 query cells whether a path through empty cells of an infinitely tiled grid reaches the origin.
Level

Hard8 of 10

Topics
Union-find, Graph, BFS
Solved
No attempts yet

Problem

A labyrinth covers the whole plane. It is built from a pattern, a rectangular grid of nn rows and mm columns in which every cell is either empty or blocked. Copying the pattern in all four directions gives an infinite grid of cells.

Number the rows and the columns of the infinite grid with integers, negative numbers included. Row numbers grow downwards and column numbers grow to the right. The cell at coordinates (0,0)(0, 0) is the origin. Every copy of the pattern fills an nn by mm rectangle whose upper left cell has a row number divisible by nn and a column number divisible by mm, and no copy is mirrored or rotated. The upper left cell of the pattern therefore lands on the origin, and its lower right cell lands on the cell with coordinates (n−1,m−1)(n - 1, m - 1).

Escaping the labyrinth from a cell means reaching the origin through empty cells, moving one step up, down, left or right at a time.

You are given the pattern and a list of starting cells. For each starting cell, decide whether escaping is possible.

Input

The first line has two integers nn and mm (1≤n,m≤1001 \le n, m \le 100), the number of rows and the number of columns of the pattern. Each of the next nn lines has a string of exactly mm characters describing one row of the pattern. The character # marks a blocked cell and . marks an empty cell.

The next line has an integer qq (1≤q≤2000001 \le q \le 200000), the number of starting cells. The kk-th of the next qq lines has two integers rr and cc (−109≤r,c≤109-10^9 \le r, c \le 10^9), the row and the column of the kk-th starting cell.

The origin and every starting cell are empty.

Output

Print qq lines. Line kk holds yes if the labyrinth can be escaped from the kk-th starting cell, and no otherwise.

Hint

The picture shows the labyrinth of the first example. The shaded rectangle is the copy of the pattern that starts at the origin, and the cell marked x is the origin.

Examples2

  1. Example 1

    Input
    6 9
    ..#####..
    ..#...#..
    ......#..
    ..#####..
    ..#......
    ..#...#..
    5
    1 4
    5 4
    1 -5
    5 -5
    -1000000000 0
    
    Expected output
    yes
    no
    no
    yes
    yes
    
  2. Example 2

    Input
    3 3
    ..#
    .##
    ###
    3
    0 0
    1 0
    3 4
    
    Expected output
    yes
    yes
    no