The problem statement looms below, filling you with determination.

Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up.

Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't.

You are given a grid $a$ of dimensions $n \times m$ , i. e. a grid with $n$ rows and $m$ columns. You need to answer $q$ queries ($1 \leq q \leq 2 \cdot 10^5$). Each query gives two integers $x_1, x_2$ ($1 \leq x_1 \leq x_2 \leq m$) and asks whether the subgrid of $a$ consisting of the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable.


-----Input-----

The first line contains two integers $n, m$ ($1 \leq n, m \leq 10^6$, $nm \leq 10^6$)  — the dimensions of the grid $a$.

$n$ lines follow. The $y$-th line contains $m$ characters, the $x$-th of which is 'X' if the cell on the intersection of the the $y$-th row and $x$-th column is filled and "." if it is empty.

The next line contains a single integer $q$ ($1 \leq q \leq 2 \cdot 10^5$)  — the number of queries.

$q$ lines follow. Each line contains two integers $x_1$ and $x_2$ ($1 \leq x_1 \leq x_2 \leq m$), representing a query asking whether the subgrid of $a$ containing the columns $x_1, x_1 + 1, \ldots, x_2 - 1, x_2$ is determinable.


-----Output-----

For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted).


-----Examples-----

Input
4 5
..XXX
...X.
...X.
...X.
5
1 3
3 3
4 5
5 5
1 5
Output
YES
YES
NO
YES
NO


-----Note-----

For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise.

For the first query:

..X EEN

... EEE

... EEE

... EEE

For the second query:

X N

. E

. E

. E

Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid.

For the third query:

XX NN

X. NN

X. NN

X. NN

This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well:

XX

XX

XX

XX

For the fourth query:

X N

. E

. E

. E

For the fifth query:

..XXX EENNN

...X. EEENN

...X. EEENN

...X. EEENN

This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well:

..XXX

...XX

...XX

...XX

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Data structures
Skill types: Dynamic programming

Data structures

Starter code:

Solutions:
import sys
inpu = sys.stdin.readline
prin = sys.stdout.write
(n, m) = map(int, inpu().split())
matrica = []
losekolone = [0 for i in range(m + 1)]
for i in range(0, n):
	matrica.append(inpu())
if n != 1 and m != 1:
	for j in range(0, m - 1):
		for i in range(1, n):
			if matrica[i][j] == 'X' and matrica[i - 1][j + 1] == 'X':
				losekolone[j + 1] += 1
				break
		losekolone[j + 1] += losekolone[j]
q = int(inpu())
for i in range(0, q):
	(xj, xd) = map(int, inpu().split())
	if losekolone[xj - 1] == losekolone[xd - 1]:
		prin('YES\n')
	else:
		prin('NO\n')


import sys
import bisect
input = sys.stdin.readline

def SieveOfEratosthenes(n):
	arr = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	for p in range(n + 1):
		if prime[p]:
			arr.append(p)
	return arr

def solve():
	(n, m) = map(int, input().split())
	a = []
	for _ in range(n):
		a.append(input())
	v = []
	for j in range(1, m):
		for i in range(1, n):
			if a[i][j - 1] == 'X' and a[i - 1][j] == 'X':
				v.append(j)
				break
	v.append(m)
	q = int(input())
	for i in range(q):
		(x, y) = map(int, input().split())
		lower_bound = bisect.bisect_left(v, x, 0, len(v))
		if v[lower_bound] < y:
			print('NO')
		else:
			print('YES')
t = 1
for _ in range(t):
	k = solve()


import sys
inpu = sys.stdin.readline
prin = sys.stdout.write
(n, m) = map(int, inpu().split())
strs = []
for i in range(n):
	strs.append(inpu())
bycol = [0] * m
for i in range(1, n):
	for j in range(1, m):
		if strs[i - 1][j] == 'X' and strs[i][j - 1] == 'X':
			bycol[j] += 1
sums = [bycol[0]]
for i in range(1, m):
	sums.append(sums[-1] + bycol[i])
q = int(inpu())
for i in range(q):
	(a, b) = map(int, inpu().split())
	if a == m:
		prin('YES\n')
	elif sums[b - 1] - sums[a - 1] == 0:
		prin('YES\n')
	else:
		prin('NO\n')


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
strs = [input() for i in range(n)]
bycol = [0] * m
for i in range(1, n):
	for j in range(1, m):
		if strs[i - 1][j] == 'X' and strs[i][j - 1] == 'X':
			bycol[j] += 1
sums = [bycol[0]]
for i in range(1, m):
	sums.append(sums[-1] + bycol[i])
for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a == m:
		print('YES')
	else:
		print('YES') if sums[b - 1] - sums[a - 1] == 0 else print('NO')


from sys import stdin
input = stdin.readline
(n, m) = map(int, input().split())
s = [input() for i in range(n)]
q = int(input())
pre = [0] * m
for i in range(1, n):
	for j in range(1, m):
		if s[i - 1][j] == 'X' and s[i][j - 1] == 'X':
			pre[j] = 1
for i in range(1, m):
	pre[i] += pre[i - 1]
for i in range(q):
	(l, r) = map(int, input().split())
	if pre[r - 1] > pre[l - 1]:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
s = [input() for i in range(n)]
f = [0, 0]
acc = 0
for j in range(1, m):
	for i in range(n - 1):
		if s[i][j] == 'X' and s[i + 1][j - 1] == 'X':
			acc += 1
			break
	f.append(acc)
k = int(input())
for i in range(k):
	(a, b) = map(int, input().split())
	print('YES' if f[b] - f[a] == 0 else 'NO')


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
grid = []
for _ in range(n):
	grid.append(input())
cumsum_issue_list = [0] * m
for i in range(1, n):
	for j in range(1, m):
		if grid[i][j - 1] == 'X' and grid[i - 1][j] == 'X':
			cumsum_issue_list[j] += 1
for i in range(1, m):
	cumsum_issue_list[i] += cumsum_issue_list[i - 1]
q = int(input())
for _ in range(q):
	(x1, x2) = map(int, input().split())
	if cumsum_issue_list[x2 - 1] - cumsum_issue_list[x1 - 1] == 0:
		print('YES')
	else:
		print('NO')


import math
from heapq import heappush, heappop, heapify
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
(n, m) = M()
l = []
for i in range(n):
	l.append(S())
dp = [0] * (m + 1)
for i in range(1, n):
	for j in range(m - 1):
		if l[i][j] == 'X' and l[i - 1][j + 1] == 'X':
			dp[j + 1] = 1
for i in range(1, m + 1):
	dp[i] += dp[i - 1]
for i in range(I()):
	(l, r) = M()
	if dp[r - 1] - dp[l - 1]:
		print('NO')
	else:
		print('YES')


from sys import stdin
read = stdin.readline
(n, m) = map(int, read().split())
grid = []
for i in range(n):
	grid.append(read())
undet = [0 for i in range(m + 1)]
for i in range(n):
	for j in range(m):
		if (i + 1 < n and j + 1 < m) and grid[i + 1][j] == 'X' and (grid[i][j + 1] == 'X'):
			undet[j + 2] = 1
for i in range(1, m + 1):
	undet[i] += undet[i - 1]
for _ in range(int(read())):
	(l, r) = map(int, read().split())
	if l == r:
		print('YES')
		continue
	if undet[r] - undet[l] == 0:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
grid = []
for i in range(n):
	grid.append(list(input()))
canMove = [1] * m
for i in range(1, m):
	for j in range(1, n):
		if grid[j - 1][i] == 'X' and grid[j][i - 1] == 'X':
			canMove[i] = 0
canMovePrefix = [0] * m
canMovePrefix[0] = canMove[0]
for i in range(1, m):
	canMovePrefix[i] = canMovePrefix[i - 1] + canMove[i]
queries = int(input())
for i in range(queries):
	(a, b) = map(int, input().split())
	if canMovePrefix[b - 1] - canMovePrefix[a - 1] != b - a:
		print('NO')
	else:
		print('YES')


from sys import stdin
import math
input = stdin.readline
(n, m) = map(int, input().split())
inputs = []
for _ in range(n):
	inputs.append(input().strip())
dp = [[-1] * m for _ in range(n)]
for i in range(m):
	dp[0][i] = i if inputs[0][i] == 'X' else -1
for i in range(n):
	dp[i][0] = 0 if inputs[i][0] == 'X' else -1
arr = [-1] * m
for i in range(1, n):
	for j in range(1, m):
		dp[i][j] = min(dp[i - 1][j], dp[i][j - 1])
		if dp[i][j] != -1:
			arr[j] = max(arr[j], dp[i][j])
		if inputs[i][j] == 'X':
			dp[i][j] = j
for i in range(1, m):
	arr[i] = max(arr[i], arr[i - 1])
for _ in range(int(input())):
	(c1, c2) = map(int, input().split())
	if arr[c2 - 1] < c1 - 1:
		print('YES')
	else:
		print('NO')