import sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
f=[]
for i in range(n):
f.append(list(map(int,input().split())))
i,j=0,0
h=[0 for i in range(n)]
v=[0 for i in range(m)]
while True:
if j<m-1 and f[i][j+1]==0:
j+=1
h[i]=j
f[i][j]+=2
elif f[i+1][j]==0:
i+=1
h[i]=j
f[i][j]+=10
elif f[i][j]==2:
f[i][j]=1
j-=1
h[i]=j
else:
f[i][j]=1
i-=1
if i==n-1 and j==m-1:
break
i,j=0,0
while True:
if i<n-1 and f[i+1][j]%2==0:
i+=1
v[j]=i
f[i][j]+=4
elif f[i][j+1]%2==0:
j+=1
v[j]=i
f[i][j]+=20
elif f[i][j]//16==0:
f[i][j]=1
i-=1
v[j]=i
else:
f[i][j]=1
j-=1
if i==n-1 and j==m-1:
break
q=int(input())
for _ in range(q):
i,j=list(map(int,input().split()))
i-=1
j-=1
if j>h[i] or i>v[j]:
print(1)
elif f[i][j]%8==6:
print(0)
elif f[i][j]%8==0:
print(1)
else:
print(1)
if f[i][j]%8==2:
if f[i][j]==2:
f[i][j]=1
j-=1
h[i]=j
else:
f[i][j]=1
while True:
while j<m-1 and f[i][j+1]%2==0:
j+=1
if (f[i][j]//2)%2==1:
break
h[i]=max(h[i],j)
if (f[i][j]//2)%2==1:
f[i][j]-=8
break
else:
f[i][j]+=10
if i<n-1 and (f[i+1][j]//8)%2==0:
i+=1
h[i]=j
f[i][j]+=10
elif (f[i][j]//8)%2==1:
f[i][j]=1
j-=1
h[i]=j
else:
f[i][j]=1
i-=1
else:
if f[i][j]==4:
f[i][j]=1
i-=1
v[j]=i
else:
f[i][j]=1
while True:
while i<n-1 and f[i+1][j]%2==0:
i+=1
if (f[i][j]//4)%2==1:
break
v[j]=max(v[j],i)
if (f[i][j]//16)%2==1:
f[i][j]-=16
break
else:
f[i][j]+=20
if j<m-1 and (f[i][j+1]//16)%2==0:
j+=1
v[j]=i
f[i][j]+=20
elif (f[i][j]//16)%2==1:
f[i][j]=1
i-=1
v[j]=i
else:
f[i][j]=1
j-=1
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
3172 KB |
Output is correct |
2 |
Incorrect |
29 ms |
3412 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
3172 KB |
Output is correct |
2 |
Incorrect |
29 ms |
3412 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |