elite-forces

Let's tell you the legendary story of two famous kings William and Harold . Both were sworn enemies of each other & William want to conquer the Harold's city . But first William has to defeat Harold's army.

Harold has an army not a huge one but a tactical one. He has got teams in his army in the form of rectangular blocks, such that if any of them are left, William will lose the battle.

Each soldier has a rank in the army, and only a specified rectangular arrangement of them forms a tactical team. If any one of the soldiers in the team dies all the soldiers of that team die. You need to find them and kill them all .But William has limited resources and need to do this in minimum number of ammunition.

You need to find the number of missiles required to defeat Harold and kill all the tactical groups.
One missile will kill only one soldiers .

As we all know tactical team are costly so numbers of tactical team in Harold's army will be always less then equal to 100.

Input Format

2 Integers N and M denoting the dimensions of the army
Next N lines contains M integers each denoting the rank of the soldier
single integer Q denoting number of queries
each query consists
PN PM : Two Integer denoting the size of group
Next PN lines contain PM integers denoting the arrangement of the soldiers to form the tactical group

Output Format

print the minimum number of missiles to destroy all the tactical groups

Constraints:

1 ≤ N, M, PN, PM ≤ 1000
1 ≤ Q ≤ 10
1 ≤ rank ≤ 9

SAMPLE INPUT
4 4
1 2 5 3
2 5 3 3
5 3 1 1
6 8 1 7
2
2 2 
2 5
5 3
1 2
5 3

SAMPLE OUTPUT
1
3

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions: