Under construction

Still being built. Things may change

Rooms

Time limit3.5sMemory limit1024 MB

Summary
Given a letter grid, count the same-letter 4-connected regions that have at least one cell inside each query rectangle.
Level

Medium7 of 10

Topics
Graph, Matrix, Union-find
Solved
No attempts yet

Problem

You are given a matrix with NN rows and MM columns. Each cell contains one lowercase English letter.

A room is a maximal set of cells that contain the same letter and are connected in 4 directions: up, down, left, and right.

Answer QQ queries. Each query gives a rectangular submatrix, and you must count the rooms that are completely or partly inside it.

Input

The first line contains two integers NN and MM. Each of the next NN lines contains MM lowercase letters. The next line contains the integer QQ, the number of queries. Each of the following QQ lines contains four integers x1x_1, y1y_1, x2x_2, and y2y_2, which describe the rectangle whose diagonally opposite corners are (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2).

Output

Print QQ lines. The ii-th line contains the answer to the ii-th query.

Constraints

1≤N,M≤20001 \le N, M \le 2000

1≤Q≤50001 \le Q \le 5000

1≤x1,x2≤N1 \le x_1, x_2 \le N

1≤y1,y2≤M1 \le y_1, y_2 \le M

Examples1

  1. Example 1

    Input
    5 6
    aabbcc
    abbbcc
    cbeaed
    adeeed
    affttz
    3
    1 1 5 6
    2 1 4 5
    3 3 5 6
    
    Expected output
    12
    8
    6