import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); long[][] sums = new long[m + 1][m + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { sums[i][j] = sc.nextInt() + sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1]; } } int[] ans = new int[n]; int[] xArr = new int[n]; int[] yArr = new int[n]; for (int i = 0; i < n; i++) { xArr[i] = sc.nextInt(); yArr[i] = sc.nextInt(); } for (int a = 1; a <= m; a++) { HashSet targets = new HashSet<>(); for (int i = 0; i < n; i++) { if (xArr[i] >= a) { targets.add(i); } } if (targets.size() == 0) { continue; } for (int b = m; b >= a; b--) { for (int i = 0; i < n; i++) { if (xArr[i] > b) { targets.remove(i); } } if (targets.size() == 0) { continue; } for (int c = 1; c <= m; c++) { for (int i = 0; i < n; i++) { if (yArr[i] < c) { targets.remove(i); } } if (targets.size() == 0) { continue; } for (int d = c; d <= m; d++) { long sum = sums[b][d] - sums[a - 1][d] - sums[b][c - 1] + sums[a - 1][c - 1]; if (sum == 0) { for (int i = 0; i < n; i++) { if (xArr[i] >= a && xArr[i] <= b && yArr[i] >= c && yArr[i] <= d) { ans[i]++; } } } } } } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(ans[i]).append("\n"); } System.out.print(sb); } }