<?php
// paiza POH! vol.2
// result:
// http://p...content-available-to-author-only...a.jp/poh/paizen/result/d548b3ab8816ce66e9d6803dd491e75f
// author: Leonardone @ NEETSDKASU

fscanf(STDIN, "%d %d", $h, $w);

$table = array();
$cache = array();
$space2right = array();

for ($y = 0; $y < $h; $y++) {
	fscanf(STDIN, "%s", $str);
	$count = 0;
	$table[$y] = array_fill(0, $w, 0); 
	$cache[$y] = array_fill(0, $w, 0); 
	$space2right[$y] = array_fill(0, $w, 0); 
	for ($x = $w - 1; $x >= 0; $x--) {
		if ($str[$x] == '0') {
			$count++;
		} else {
			$count = 0;
		}
		$space2right[$y][$x] = $count;
	}
}

for ($y = 0; $y < $h; $y++) {
	for ($x = 0; $x < $w; $x++) {
		if ($space2right[$y][$x] == 0) {
			continue;
		}
		$s = 1;
		$t = $space2right[$y][$x];
		for ($i = $y; $i < $h && $space2right[$i][$x] > 0; $i++) {
			if ($space2right[$i][$x] < $t) {
				$t = $space2right[$i][$x];
			}
			$table[$s][$t]++;
			$s++;
		}
	}
}

fscanf(STDIN, "%d", $n);

for ($i = 0; $i < $n; $i++) {
	fscanf(STDIN, "%d %d", $s, $t);
	if ($s > $h || $t > $w) {
		echo '0', PHP_EOL;
	} else {
		if ($cache[$s][$t] > 0) {
			echo ($cache[$s][$t] - 1), PHP_EOL;
		} else {
			$count = 0;
			for ($j = $t; $j <= $w; $j++) {
				$count += $table[$s][$j];
			}
			$cache[$s][$t] = $count + 1;
			echo $count, PHP_EOL;
		}
	}
}
