<?php
// H, W取得
list($height, $width) = explode(' ', trim(fgets(STDIN)));

// D取得
for ($h = 0; $h < $height; $h++) {
	$home[] = trim(fgets(STDIN));
}

// N取得
$cnt = trim(fgets(STDIN));

// 指定座標を左上として入る大きさを探索する
function search($h, $w) {
	global $home, $available, $height, $width;
	$available[1][1]++;
	$t_max = $width - $w;
	for ($s = 1; $s <= $height - $h; $s++) {
		for ($t = (1 === $s) ? 2 : 1; $t <= $t_max; $t++) {
			if ('0' === substr($home[$h + $s - 1], $w + $t - 1, 1)) {
				$available[$s][$t]++;
			} else if (1 === $t) {
				return;
			} else {
				$t_max = $t - 1;
				break;
			}
		}
	}
}

// 探索用データ初期化
$available = array();
for ($s = 1; $s <= $height; $s++) {
	for ($t = 1; $t <= $width; $t++) {
		$available[$s][$t] = 0;
	}
}

// 検索用データ生成
for ($h = 0; $h < $height; $h++) {
	for ($w = 0; $w < $width; $w++) {
		if (substr($home[$h], $w, 1) === '0') {
			search($h, $w);
		}
	}
}

// S, T取得
$output = '';
for ($n = 0; $n < $cnt; $n++) {
	list($h, $w) = explode(' ', trim(fgets(STDIN)));

	if ($h > $height or $w > $width) {
		$output .= "0\n";
	} else {
		$output .= $available[$h][$w]."\n";
	}
}
echo $output;