Rooms
시간 제한3.5초메모리 제한1024 MB
알파벳 격자에서 같은 글자가 상하좌우로 연결된 방들을 구하고, 각 직사각형 질의에 겹치는 방의 개수를 센다.
문제
You are given a matrix with rows and columns which contains lowercase letters of the English alphabet.
A room is a maximal component of cells which share the same letter and which are connected in directions: up, down, left, right.
You have to answer queries of the type: "How many rooms are completely or partly included in a given rectangular submatrix?".
입력
The first line contains two integers and .
Each of the following lines contains lowercase letters.
On the following line there is an integer indicating the number of queries.
The next lines contain integers , , , , denoting a rectangle formed by the diagonally opposite points of coordinates and .
출력
You should output lines, each containing the answer for a query.