#include <bits/stdc++.h>
using namespace std;
namespace mine {
long long qread() {
  long long ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(long long num) {
  if (num < 0) putchar('-'), num = -num;
  if (num >= 10) write(num / 10);
  putchar('0' + num % 10);
}
void write1(long long num) {
  write(num);
  putchar(' ');
}
void write2(long long num) {
  write(num);
  putchar('\n');
}
template <typename T>
inline bool chmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool chmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
bool IN(long long x, long long l, long long r) { return l <= x and x <= r; }
void gg1() {
  write2(-1);
  exit(0);
}
void gg2() {
  puts("NO");
  exit(0);
}
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T>
void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
long long qpower(long long x, long long e, int mod = MOD) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int N = 1e6 + 10, M = 3e2 + 10;
int n, m, a[M][M], b[M][M];
int id(int x, int y) { return (x - 1) * m + y; }
struct DSU {
  int fa[M * M], cnt, siz[M * M];
  void clear() {
    for (int i = (1), I = (n * m); i <= I; i++) fa[i] = i, siz[i] = 1;
  }
  int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
  void merg(int x, int y) {
    x = findfa(x), y = findfa(y);
    if (siz[x] > siz[y]) swap(x, y);
    if (x != y) cnt--, fa[x] = y, siz[y] += siz[x];
  }
} dsu, dsu2;
const int tx[] = {0, 1, 0, -1};
const int ty[] = {1, 0, -1, 0};
void upd(int i, int j) {
  for (int t = (0), I = (3); t <= I; t++) {
    int ii = i + tx[t], jj = j + ty[t];
    if (ii < 1 or jj < 1 or ii > n or jj > m) continue;
    if (a[i][j] == a[ii][jj]) dsu.merg(id(i, j), id(ii, jj));
  }
}
vector<pair<pair<int, int>, int> > in;
vector<int> ans2;
int lstc = 0;
void fuk() {
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++) b[i][j] = a[i][j];
  dsu.clear();
  dsu.cnt = 0;
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++)
      if (a[i][j] != lstc) dsu.cnt++;
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++)
      if (a[i][j] != lstc) upd(i, j);
  vector<int> ans;
  int ln = ((int)(in).size());
  for (int t = (ln - 1), I = (0); t >= I; t--) {
    int x = in[t].first.first, y = in[t].first.second, bk = in[t].second;
    ans.push_back(dsu.cnt);
    if (a[x][y] != bk) dsu.cnt++, a[x][y] = bk, upd(x, y);
  }
  reverse((ans).begin(), (ans).end());
  if (lstc)
    for (int t = (0), I = (ln - 1); t <= I; t++) write2(ans[t] + ans2[t]);
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++) a[i][j] = b[i][j];
  in.clear();
  ans2.clear();
  dsu2.cnt = 0;
  dsu2.clear();
}
void main() {
  n = qread(), m = qread();
  int q = qread();
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++) in.push_back({{i, j}, 0});
  int cnt = 0;
  while (q--) {
    int i = qread(), j = qread(), c = qread();
    if (lstc != c) fuk(), cnt++;
    in.push_back({{i, j}, a[i][j]});
    if (a[i][j] != c) {
      dsu2.cnt++;
      a[i][j] = c;
      lstc = c;
      for (int t = (0), I = (3); t <= I; t++) {
        int ii = i + tx[t], jj = j + ty[t];
        if (ii < 1 or jj < 1 or ii > n or jj > m) continue;
        if (a[i][j] == a[ii][jj]) dsu2.merg(id(i, j), id(ii, jj));
      }
    }
    ans2.push_back(dsu2.cnt);
  }
  fuk();
}
};  // namespace mine
signed main() {
  srand(time(0));
  mine::main();
  if (0) cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
}

### Prompt

In Cpp, your task is to solve the following 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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
namespace mine {
long long qread() {
  long long ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(long long num) {
  if (num < 0) putchar('-'), num = -num;
  if (num >= 10) write(num / 10);
  putchar('0' + num % 10);
}
void write1(long long num) {
  write(num);
  putchar(' ');
}
void write2(long long num) {
  write(num);
  putchar('\n');
}
template <typename T>
inline bool chmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool chmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
bool IN(long long x, long long l, long long r) { return l <= x and x <= r; }
void gg1() {
  write2(-1);
  exit(0);
}
void gg2() {
  puts("NO");
  exit(0);
}
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T>
void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
long long qpower(long long x, long long e, int mod = MOD) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int N = 1e6 + 10, M = 3e2 + 10;
int n, m, a[M][M], b[M][M];
int id(int x, int y) { return (x - 1) * m + y; }
struct DSU {
  int fa[M * M], cnt, siz[M * M];
  void clear() {
    for (int i = (1), I = (n * m); i <= I; i++) fa[i] = i, siz[i] = 1;
  }
  int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
  void merg(int x, int y) {
    x = findfa(x), y = findfa(y);
    if (siz[x] > siz[y]) swap(x, y);
    if (x != y) cnt--, fa[x] = y, siz[y] += siz[x];
  }
} dsu, dsu2;
const int tx[] = {0, 1, 0, -1};
const int ty[] = {1, 0, -1, 0};
void upd(int i, int j) {
  for (int t = (0), I = (3); t <= I; t++) {
    int ii = i + tx[t], jj = j + ty[t];
    if (ii < 1 or jj < 1 or ii > n or jj > m) continue;
    if (a[i][j] == a[ii][jj]) dsu.merg(id(i, j), id(ii, jj));
  }
}
vector<pair<pair<int, int>, int> > in;
vector<int> ans2;
int lstc = 0;
void fuk() {
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++) b[i][j] = a[i][j];
  dsu.clear();
  dsu.cnt = 0;
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++)
      if (a[i][j] != lstc) dsu.cnt++;
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++)
      if (a[i][j] != lstc) upd(i, j);
  vector<int> ans;
  int ln = ((int)(in).size());
  for (int t = (ln - 1), I = (0); t >= I; t--) {
    int x = in[t].first.first, y = in[t].first.second, bk = in[t].second;
    ans.push_back(dsu.cnt);
    if (a[x][y] != bk) dsu.cnt++, a[x][y] = bk, upd(x, y);
  }
  reverse((ans).begin(), (ans).end());
  if (lstc)
    for (int t = (0), I = (ln - 1); t <= I; t++) write2(ans[t] + ans2[t]);
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++) a[i][j] = b[i][j];
  in.clear();
  ans2.clear();
  dsu2.cnt = 0;
  dsu2.clear();
}
void main() {
  n = qread(), m = qread();
  int q = qread();
  for (int i = (1), I = (n); i <= I; i++)
    for (int j = (1), I = (m); j <= I; j++) in.push_back({{i, j}, 0});
  int cnt = 0;
  while (q--) {
    int i = qread(), j = qread(), c = qread();
    if (lstc != c) fuk(), cnt++;
    in.push_back({{i, j}, a[i][j]});
    if (a[i][j] != c) {
      dsu2.cnt++;
      a[i][j] = c;
      lstc = c;
      for (int t = (0), I = (3); t <= I; t++) {
        int ii = i + tx[t], jj = j + ty[t];
        if (ii < 1 or jj < 1 or ii > n or jj > m) continue;
        if (a[i][j] == a[ii][jj]) dsu2.merg(id(i, j), id(ii, jj));
      }
    }
    ans2.push_back(dsu2.cnt);
  }
  fuk();
}
};  // namespace mine
signed main() {
  srand(time(0));
  mine::main();
  if (0) cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
}

```