#P6504. [COCI 2010/2011 #3] MONO

[COCI 2010/2011 #3] MONO

Description

You are given an r×cr \times c matrix consisting of lowercase letters. Each cell contains one letter. In this setting:

  • The top-left corner has coordinates (0,0)(0,0).
  • The top-right corner has coordinates (c,0)(c,0).
  • The bottom-left corner has coordinates (0,r)(0,r).
  • The bottom-right corner has coordinates (c,r)(c,r).

Note that these coordinates refer to grid points, while letters are placed inside cells.

You are given a polygon whose vertices lie on some grid points, and whose edges are all parallel to the sides of the matrix. Please determine, after translating this polygon up, down, left, and right, what is the maximum number of different positions such that all letters inside the polygon are the same.

Input Format

The first line contains two integers r,cr, c, the height and width of the rectangle.

The next rr lines each contain cc letters, describing the matrix.

The next line contains an integer VV, the number of vertices of the polygon.

The next VV lines each contain two integers x,yx, y, representing the coordinates of one vertex of the polygon. The vertices are given in clockwise order.

Output Format

Output one integer in a single line, representing the number of translated polygon positions whose interior letters are all the same.

3 3
aaa
aaa
aaa
4
2 0
2 2
0 2
0 0
4
3 3
aaa
aba
aaa
4
2 0
2 2
0 2
0 0
0
5 4
xyyx
xyyy
xxyy
xxxx
xxxx
8
1 3
1 2
0 2
0 0
2 0
2 1
3 1
3 3
2

Hint

Constraints

  • For 40%40\% of the testdata, r,c,V20r, c, V \le 20.
  • For 70%70\% of the testdata, V20V \le 20.
  • For 100%100\% of the testdata, 1r,c5001 \le r, c \le 500, 4V5004 \le V \le 500, 0xc0 \le x \le c, 0yr0 \le y \le r.

Notes

This problem is translated from COCI2010-2011 CONTEST #3 T6 MONO

Translated by ChatGPT 5