Problem: You are given a matrix n × m, initially filled with zeroes. We define a_{i, j} as the element in the i-th row and the j-th column of the matrix.

Two cells of the matrix are connected if they share a side, and the elements in these cells are equal. Two cells of the matrix belong to the same connected component if there exists a sequence s_1, s_2, ..., s_k such that s_1 is the first cell, s_k is the second cell, and for every i ∈ [1, k - 1], s_i and s_{i + 1} are connected.

You are given q queries of the form x_i y_i c_i (i ∈ [1, q]). For every such query, you have to do the following:

  1. replace the element a_{x, y} with c; 
  2. count the number of connected components in the matrix. 



There is one additional constraint: for every i ∈ [1, q - 1], c_i ≤ c_{i + 1}.

Input

The first line contains three integers n, m and q (1 ≤ n, m ≤ 300, 1 ≤ q ≤ 2 ⋅ 10^6) — the number of rows, the number of columns and the number of queries, respectively.

Then q lines follow, each representing a query. The i-th line contains three integers x_i, y_i and c_i (1 ≤ x_i ≤ n, 1 ≤ y_i ≤ m, 1 ≤ c_i ≤ max(1000, ⌈ (2 ⋅ 10^6)/(nm) ⌉)). For every i ∈ [1, q - 1], c_i ≤ c_{i + 1}.

Output

Print q integers, the i-th of them should be equal to the number of components in the matrix after the first i queries are performed.

Example

Input


3 2 10
2 1 1
1 2 1
2 2 1
1 1 2
3 1 2
1 2 2
2 2 2
2 1 2
3 2 4
2 1 5


Output


2
4
3
3
4
4
4
2
2
4

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int nax = 333;
int a[nax][nax];
struct qq {
  int r;
  int c;
  int v;
};
int n, m, q;
bool exist[2 * 1000 * 1000 + 5];
int ans[2 * 1000 * 1000 + 5];
int res[2 * 1000 * 1000 + 5];
pair<int, int> par[nax][nax];
pair<int, int> f(int r, int c) {
  if (par[r][c] == make_pair(r, c)) return par[r][c];
  pair<int, int> p = par[r][c];
  return par[r][c] = f(p.first, p.second);
}
int u(int r1, int c1, int r2, int c2) {
  if (f(r1, c1) == f(r2, c2)) return 0;
  pair<int, int> p1 = f(r1, c1);
  pair<int, int> p2 = f(r2, c2);
  par[p1.first][p1.second] = p2;
  return 1;
}
int cur;
set<pair<int, int> > base[2 * 1000 * 1000 + 5];
set<int> ti[nax][nax];
vector<pair<int, pair<int, int> > > del[2 * 1000 * 1000 + 5];
int main() {
  scanf("%d%d%d", &n, &m, &q);
  vector<qq> s;
  for (int i = 1; i <= q; i++) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    qq p;
    p.r = x;
    p.c = y;
    p.v = v;
    ti[x][y].insert(i);
    s.push_back(p);
    exist[v] = true;
  }
  vector<int> dx = {-1, 1, 0, 0};
  vector<int> dy = {0, 0, -1, 1};
  for (int i = 0; i < q; i++) {
    int l = i;
    int r = i;
    while (r + 1 < q && s[r + 1].v == s[r].v) r++;
    for (int j = l; j <= r; j++) {
      cur = 0;
      auto akt = s[j];
      int r = akt.r;
      int c = akt.c;
      int col = akt.v;
      if (a[r][c] == col) continue;
      a[r][c] = col;
      cur++;
      par[r][c] = make_pair(r, c);
      for (int z = 0; z < 4; z++) {
        int ar = r + dx[z];
        int ac = c + dy[z];
        if (ar < 1 || ar > n || ac < 1 || ac > m) continue;
        if (a[ar][ac] == col) cur -= u(r, c, ar, ac);
      }
      ans[j + 1] = cur;
    }
    set<pair<int, int> > done;
    set<pair<int, int> > cells;
    int col = s[l].v;
    for (int j = r; j >= l; j--) {
      auto akt = s[j];
      if (done.count(make_pair(akt.r, akt.c))) continue;
      done.insert(make_pair(akt.r, akt.c));
      auto it = ti[akt.r][akt.c].upper_bound(j + 1);
      if (it == ti[akt.r][akt.c].end()) {
        base[col].insert({akt.r, akt.c});
      } else {
        del[col].push_back(make_pair(*it, make_pair(akt.r, akt.c)));
      }
    }
    i = r;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) a[i][j] = 0;
  }
  for (int i = 0; i < 2 * 1000 * 1000 + 5; i++) {
    if (exist[i] == false) continue;
    int col = i;
    sort(del[i].begin(), del[i].end());
    for (auto bro : base[col]) {
      if (a[bro.first][bro.second] == col) continue;
      a[bro.first][bro.second] = col;
      par[bro.first][bro.second] = make_pair(bro.first, bro.second);
      int x = bro.first;
      int y = bro.second;
      for (int z = 0; z < 4; z++) {
        int ax = x + dx[z];
        int ay = y + dy[z];
        if (ax < 1 || ax > n || ay < 1 || ay > m) continue;
        if (a[ax][ay] == col) {
          u(x, y, ax, ay);
        }
      }
    }
    for (int j = (int)del[i].size() - 1; j >= 0; j--) {
      auto akt = del[i][j];
      int x = akt.second.first;
      int y = akt.second.second;
      par[x][y] = make_pair(0, 0);
    }
    for (int j = (int)del[i].size() - 1; j >= 0; j--) {
      auto akt = del[i][j];
      int cz = akt.first;
      int x = akt.second.first;
      int y = akt.second.second;
      if (par[x][y] == make_pair(x, y)) continue;
      par[x][y] = make_pair(x, y);
      a[x][y] = col;
      int cur = 1;
      for (int z = 0; z < 4; z++) {
        int ax = x + dx[z];
        int ay = y + dy[z];
        if (ax < 1 || ax > n || ay < 1 || ay > m) continue;
        if (a[ax][ay] == col) {
          cur -= u(x, y, ax, ay);
        }
      }
      cur *= -1;
      ans[cz] += cur;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      a[i][j] = 1;
      par[i][j] = make_pair(0, 0);
    }
  }
  vector<pair<int, int> > bas;
  vector<pair<int, pair<int, int> > > de;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ti[i][j].size() == 0)
        bas.push_back(make_pair(i, j));
      else {
        de.push_back(make_pair(*ti[i][j].begin(), make_pair(i, j)));
      }
    }
  }
  sort(de.begin(), de.end());
  ans[1]++;
  for (auto bro : bas) {
    int x = bro.first;
    int y = bro.second;
    par[x][y] = make_pair(x, y);
    a[x][y] = 0;
    for (int z = 0; z < 4; z++) {
      int ax = x + dx[z];
      int ay = y + dy[z];
      if (ax < 1 || ax > n || ay < 1 || ay > m) continue;
      if (a[ax][ay] == 0) {
        u(x, y, ax, ay);
      }
    }
  }
  for (int i = (int)de.size() - 1; i >= 0; i--) {
    auto akt = de[i];
    int cz = akt.first;
    int x = akt.second.first;
    int y = akt.second.second;
    if (par[x][y] == make_pair(x, y)) continue;
    par[x][y] = make_pair(x, y);
    a[x][y] = 0;
    int cur = 1;
    for (int z = 0; z < 4; z++) {
      int ax = x + dx[z];
      int ay = y + dy[z];
      if (ax < 1 || ax > n || ay < 1 || ay > m) continue;
      if (a[ax][ay] == 0) {
        cur -= u(x, y, ax, ay);
      }
    }
    cur *= -1;
    ans[cz] += cur;
  }
  for (int i = 1; i <= q; i++) {
    ans[i] += ans[i - 1];
    printf("%d\n", ans[i]);
  }
  return 0;
}