Submit | All submissions | Best solutions | Back to list |
MONO - MONO |
Mirko soon realised that number sequences are not the best career choice, and went right back to letter table business.
Mirko’s table has R rows and C columns and consists of lowercase letters. Each cell of the table is a square of equal size. We assign coordinates to vertices of those squares, so that upper-left corner of the table has coordinates (0, 0), upper-right (C, 0), lower-left (0, R), and lower-right (C, R).
We say that polygon within the table is monoliteral if the following holds:
- its vertices are from the described set of cell-square vertices,
- its edges are parallel to coordinate axes,
- all letters inside the polygon are equal.
A simple polygon for which first two conditions are true (third one may or may not be true) is given.
Mirko would like to know the number of monoliteral polygons that can be obtained by moving the given one up, down, left, or right or any combination thereof, but not rotating.
Input
The first line of input contains two space separated integers R and C (1 ≤ R, C ≤ 500).
Each of the next R lines contains exactly C lowercase letters, this is Mirko’s table.
The following line contains integer V (4 ≤ V ≤ 500), number of vertices of given polygon.
Each of the next V lines contains two integers X, Y (0 ≤ X ≤ C, 0 ≤ Y ≤ R). These are the coordinates
of the vertices of the given polygon. Vertices are given in clockwise order.
The given polygon will satisfy conditions 1 and 2 from above.
Output
In the first and only line of output, print expected number of polygons.
Examples
Input: 3 3 aaa aaa aaa 4 2 0 2 2 0 2 0 0 Output: 4
Input: 3 3 aaa aba aaa 4 2 0 2 2 0 2 0 0 Output: 0
Input: 5 4 xyyx xyyy xxyy xxxx xxxx 8 1 3 1 2 0 2 0 0 2 0 2 1 3 1 3 3 Output: 2
Added by: | islam |
Date: | 2012-02-13 |
Time limit: | 0.100s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | COCI 2010/2011 Contest 3 by Luka Kalinovčić |
hide comments
2023-09-19 20:04:12
Remove please this time limit to hard |
|
2020-01-12 23:02:48 Marin Kisic
Time limit is too strict. On the contest it was 5 sec, probably now should be 1 sec. |