import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); long[][] field = new long[m + 1][m + 1]; for (int i = 1; i <= m; i++) { String[] line = br.readLine().split(" ", m); for (int j = 1; j <= m; j++) { field[i][j] = field[i][j - 1] + field[i - 1][j] - field[i - 1][j - 1] + Integer.parseInt(line[j - 1]); } } int[] xArr = new int[n]; int[] yArr = new int[n]; for (int i = 0; i < n; i++) { String[] line = br.readLine().split(" ", 2); xArr[i] = Integer.parseInt(line[0]); yArr[i] = Integer.parseInt(line[1]); } int[] counts = new int[n]; for (int a = 1; a <= m; a++) { for (int b = 1; b <= m; b++) { for (int c = a; c <= m; c++) { for (int d = b; d <= m; d++) { if (field[c][d] - field[a - 1][d] - field[c][b - 1] + field[a- 1][b - 1] != 0) { continue; } for (int i = 0; i < n; i++) { if (xArr[i] >= a && xArr[i] <= c && yArr[i] >= b && yArr[i] <= d) { counts[i]++; } } } } } } StringBuilder sb = new StringBuilder(); for (int x : counts) { sb.append(x).append("\n"); } System.out.print(sb); } }