PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000212. Lưới gạch tuần hoàn (Tile Pattern)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lưới gạch tuần hoàn (Tile Pattern)

Tile Pattern

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A grid has 10910^9 rows and 10910^9 columns, indexed from 0. A repeating N×NN\times N tile PP uses B for black and W for white. Cell (i,j)(i,j) has color Pi mod N,j mod NP_{i\bmod N,j\bmod N}. For each query A,B,C,DA,B,C,D, count black cells in rows AA through CC and columns BB through DD, inclusive.

Input

The first line contains N,QN,Q. The next NN lines each contain exactly NN characters B or W. Each of the next QQ lines contains four integers A,B,C,DA,B,C,D.

Output

Print QQ lines, one black-cell count per query.

Subtasks

  • Subtask 1 (20 points): 1≤N≤121\le N\le 12; 1≤Q≤401\le Q\le 40; 0≤A≤C<600\le A\le C<60; 0≤B≤D<600\le B\le D<60; each tile cell is B or W.
  • Subtask 2 (30 points): 1≤N≤10001\le N\le 1000; 1≤Q≤2000001\le Q\le 200000; 0≤A≤C<10000000000\le A\le C<1000000000; 0≤B≤D<10000000000\le B\le D<1000000000; each tile cell is B or W; N=1N=1.
  • Subtask 3 (50 points): 1≤N≤10001\le N\le 1000; 1≤Q≤2000001\le Q\le 200000; 0≤A≤C<10000000000\le A\le C<1000000000; 0≤B≤D<10000000000\le B\le D<1000000000; each tile cell is B or W.

Example 1

Input

3 2
WWB
BBW
WBW
1 2 3 4
0 3 4 5

Output

4
7

Explanation

The first rectangle contains 4 black cells, the second 7. Rows and columns repeat with period 3.

Example 2

Input

10 5
BBBWWWBBBW
WWWWWBBBWB
BBBWBBWBBB
BBBWWBWWWW
WWWWBWBWBW
WBBWBWBBBB
WWBBBWWBWB
WBWBWWBBBB
WBWBWBBWWW
WWWBWWBWWB
5 21 21 93
35 35 70 43
55 72 61 84
36 33 46 95
0 0 999999999 999999999

Output

621
167
44
344
500000000000000000

Explanation

The first four counts are 621, 167, 44 and 344. The 10×10 tile has 50 black cells, and the final query consists of 101610^{16} whole tiles.