Dasha and Photos
Contest: Codeforces Round 394 (Div. 2)
Difficulty rating: 2600
Tags: brute force, data structures, dp, implementation

Dasha decided to have a rest after solving the problem D and began to look photos from previous competitions.

Let's call photos as the matrix with the size n × m, which consists of lowercase English letters.

Some k photos especially interested her, because they can be received from photo-template by painting a rectangular area in a certain color. Let's call such photos special.

More formally the i-th special photo is received from the photo-template by replacing all characters on some rectangle with upper left corner of the cell with coordinates (ai, bi) and lower right corner in the cell with coordinates (ci, di) to the symbol ei.

Dasha asks you to find the special photo so that the total distance from it to all other special photos is minimum. And calculate this distance.

Determine the distance between two photos as the sum of distances between all corresponding letters. The distance between two letters is the difference module of their positions in the alphabet. For example, the distance between letters 'h' and 'm' equals |8 - 13| = 5, because the letter 'h' is the 8-th in the alphabet, the letter 'm' is the 13-th.

Input format:
The first line contains three integers n, m, k (1 ≤ n, m ≤ 103, 1 ≤ k ≤ 3·105) — the number of strings in the photo-template, the number of columns and the number of special photos which are interesting for Dasha.

The next n lines contains the string with m length which consists of little Latin characters — the description of the photo-template.

Each of the next k lines contains the description of the special photo in the following format, "ai bi ci di ei" (1 ≤ ai ≤ ci ≤ n, 1 ≤ bi ≤ di ≤ m), where (ai, bi) — is the coordinate of the upper left corner of the rectangle, (ci, di) — is the description of the lower right corner, and ei — is the little Latin letter which replaces the photo-template in the described rectangle.

Output format:
In the only line print the minimum total distance from the found special photo to all other special photos.

Constraints:
Time limit: 4.0 s
Memory limit: 512.0 MB

Note:
In the first example the photos are following:

The distance between them is 10.

Example input:
3 3 2
aaa
aaa
aaa
1 1 2 2 b
2 2 3 3 c

Example output:
10

Example input:
5 5 3
abcde
eabcd
deabc
cdeab
bcdea
1 1 3 4 f
1 2 3 3 e
1 3 3 4 i

Example output:
59

Editorial:
Let special photo be the matrix, made by changing subrectangle in the initial, and changed submatrix — changed subrectangle itself. Firsly, let's calculate cnt(x, y, ch) — the number of special photos, in which cell (x, y) belongs to changed submatrix, such that cell (x, y) contains character ch. It can be done using addition on submatrix in offline. Then, using cnt array, let's compute f(x, y) — sum of the distances from all k photos to initial, in cell (x, y): $$f(x,y) = \sum_{i=A}^{Z} |i - a(x,y)| * cnt(x,y,i)$$. Let $$g(x,y) = \sum_{i=1}^{x} \sum_{j=1}^{y} f(i,j)$$ ( g(x, y) — sum of the distances from all k photos to initial on submatrix (1, 1, x, y). Let cnt'(x, y, ch) — the number of special photos, in which cell (x, y) contains character ch. Then calculate, similarly to g(x, y), sums $$q(x,y,ch) = \sum_{i=1}^{x} \sum_{j=1}^{y} cnt'(i,j,ch)$$. Now, using dp's alltogether, we can count for some special photo sum of the distances to all other photos : for all cells, except changed submarix, find the distance using g(x, y) and inclusion-exclusion method in O(1). For the cells in changed submatrix, let's iterate the character and find the answer for it similarly.