Rafael decided to spend his weekend at his uncle’s farm, Anthony, and when he realized that there was an apple tree there, he decided to make an apple pie.

To make the pie, however, Rafael would have to get as many apples as possible, and for that he decided to ask for help to his cousin. The deal was: His cousin would climb the tree and shake several branches containing apples, making them fall. As they would falling, Rafael would be on the ground with a basket and would get them between their travel from the tree to the ground. As the apples were falling fast, the impact with the ground would make them crack, and Rafael decided to ignore those apples that he couldn’t get in time with his basket.

We can represent the situation as follows: Rafael is positioned in an area with **N** lines and **M** columns below the tree, and can move one position horizontally, vertically or diagonally by second. Each apple falls in a given position of this area, let’s say [**i**, **j**] (**i**-th line, **j**-th column), and the exact moment that Rafael has to be at this position to get the apple is a given time **t**.

Given Rafael’s initial position, count the maximum amount of apples he can get with his basket, between all the **K** apples felled by his cousin.

## Input

There will be several test cases. Each test case starts with three integers, **N**, **M** and **K** (3 ≤ **N**, **M** ≤ 20, 1 ≤ **K**≤ 1000), representing, respectivelly, the amount of lines and columns of the area below the tree, and the number of apples fellen by his cousin.

Following there will be **K** lines, containing three integers each, **X _{i}**,

**Y**and

_{i}**T**(1 ≤

_{i}**X**≤

_{i}**N**, 1 ≤

**Y**≤

_{i}**M**, 1 ≤

**T**≤ 2*

_{i}**K**), representing, respectivelly, the line and column on which the

**i**-th apple fell, and the exact time when Rafael has to be at such position to get the apple.

The sequence of **T _{i}** values given in the input is non-decreasing, in other words,

**T**≤

_{i-1}**T**, for every 2 ≤

_{i}**i**≤

**K**. There are no two apples that fall at the same position at the same time.

Following there will be two integers **X** and **Y** (1 ≤ **X** ≤ **N**, 1 ≤ **Y** ≤ **M**), indicating the line and the column on which Rafael is at time 0.

The last test case is indicated when **N** = **M** = **K** = 0, which should not be processed.

## Output

For each test case, print one line containing one integer, representing the maximum amount of apples Rafael can get with his basket.

Sample Input | Sample Output |

5 5 3 2 2 1 3 3 2 2 4 3 1 1 5 5 4 3 2 1 3 5 2 2 5 3 1 5 4 3 3 0 0 0 |
3 3 |

#include <iostream> #include <iomanip> #include <cmath> #include <list> using namespace std; class Edge { public: Edge(int f = 0, int t = 0) : from(f), to(t) {} ~Edge() {} int from, to; void print () const { cout << '(' << from << ',' << to << ')' << endl; } }; class Graph { public: Graph() : nodes(0) {} Graph(int n) : nodes(n) {} ~Graph() { } void add_edge(int i, int j) { Edge edge(i,j); edges.push_back(edge); } int max_depth () const { int n_edges = edges.size(); int distance[nodes]; int father[nodes]; list<Edge>::const_iterator p = edges.begin(); list<Edge>::const_iterator p_end = edges.end(); for (int i = 0; i < nodes; i++) { distance[i] = -100000; father[i] = -1; } distance[0] = 0; int max = 0; for (int i = 0; i < nodes; i++) { while (p != p_end && p->from == i) { int j = p->to; if (distance[j] < distance[i] + 1) { distance[j] = distance[i] + 1; father[j] = i; } if (distance[j] > max) max = distance[j]; p++; } } return max; } void print () const { list<Edge>::const_iterator p = edges.begin(); list<Edge>::const_iterator p_end = edges.end(); cout << "Edges:" << endl; while (p != p_end) { p->print(); p++; } } int nodes; list<Edge> edges; }; void process (int k) { int apx[k], apy[k], apt[k]; for (int i = 0; i < k; i++) cin >> apx[i] >> apy[i] >> apt[i]; int x, y; cin >> x >> y; /* Edges mean that you can reach the fruit from that node, at the * given time. * Nodes are the starting point and the fruit drop points. */ Graph graph(k+1); for (int i = 0; i < k; i++) { if (max(fabs(apx[i]-x),fabs(apy[i]-y)) <= apt[i]) graph.add_edge(0,i+1); } for (int i = 0; i < k; i++) { for (int j = i+1; j < k; j++) if ( max(fabs(apx[i]-apx[j]), fabs(apy[i]-apy[j])) <= apt[j]-apt[i]) graph.add_edge(i+1,j+1); } // graph.print(); cout << graph.max_depth() << endl; } int main () { int n, m, k; cin >> n >> m >> k; while (n != 0) { process(k); cin >> n >> m >> k; } return 0; }