Killjee and famous boring problem

0

0 votes
Easy
Problem

There is a NXM grid and every cell of grid has a person or has an obstacle, now a person at Aij can move to A(i1)j, A(i+1)j, Ai(j1) and Ai(j+1), given that the box is in grid and it does not have an obstacle.

Now, you are given Q queries, In each query you are given two integers L and R. You need to find how many person can come to cell ALR.

 

INPUT

First line of input contains two integers N and M, N line follows each containing M space separated integers, elements of grid. Next line contain a single integer Q number of queries. Q lines follow each containing two space separated integer L and R.

 

OUTPUT

For each query print number of person who can visit that cell of grid.

 

CONSTRAINTS

  • 1N,M103
  • Grid will contain 0 and 1 only 1 means a person and 0 means obstacle
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?