#include <bits/stdc++.h>
using namespace std;
const int inf_int = 1e9 + 100;
const long long inf_ll = 1e18;
const double pi = 3.1415926535898;
template <class T1, class T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) {
  out << "( " << rhs.first << " , " << rhs.second << " )";
  return out;
}
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A>
string to_string(vector<vector<A>> v) {
  bool first = true;
  string res = "\n{";
  for (const auto &x : v) {
    if (!first) {
      res += ",\n ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}\n";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
bool debug = 0;
const int MAXN = 2e6 + 100;
const int LOG = 20;
const int mod = 1e9 + 7;
const int MX = (1e7 + 100);
struct event {
  int x, y;
  int L;
};
vector<event> add[MAXN];
vector<event> rem[MAXN];
int a[300][300];
int n, m;
inline int get_id(int x, int y) { return x * m + y; }
int parent[MAXN];
int get_parent(int v) {
  if (parent[v] == v) return v;
  return parent[v] = get_parent(parent[v]);
}
bool union_set(int a, int b) {
  a = get_parent(a);
  b = get_parent(b);
  if (a != b) {
    parent[a] = b;
    return true;
  }
  return false;
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
inline bool is_ok(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != -1;
}
int dif[MAXN];
void process(vector<event> &L, int coef) {
  if (L.empty()) return;
  for (int i = 0; i < n; ++i) {
    for (int e = 0; e < m; ++e) {
      a[i][e] = -1;
    }
  }
  for (int i = 0; i < n * m + 10; ++i) {
    parent[i] = i;
  }
  for (auto cur : L) {
    int x = cur.x, y = cur.y;
    int comp = 1;
    a[x][y] = 1;
    int v1 = get_id(x, y);
    for (int j = 0; j < 4; ++j) {
      int x1 = x + dx[j];
      int y1 = y + dy[j];
      int v2 = get_id(x1, y1);
      if (is_ok(x1, y1)) {
        if (union_set(v1, v2)) {
          comp--;
        }
      }
    }
    dif[cur.L] += comp * coef;
  }
}
void solve() {
  cin >> n >> m;
  int q;
  cin >> q;
  int mx = 0;
  for (int i = 0; i < q; ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    if (a[x][y] == c) {
      continue;
    }
    mx = max(mx, c);
    event temp = {x, y, i + 1};
    rem[a[x][y]].emplace_back(temp);
    a[x][y] = c;
    add[c].emplace_back(temp);
  }
  for (int i = 0; i < n; ++i) {
    for (int e = 0; e < m; ++e) {
      event temp = {i, e, q + 1};
      rem[a[i][e]].emplace_back(temp);
    }
  }
  for (int i = 0; i <= mx; ++i) {
    process(add[i], +1);
  }
  for (int i = 0; i <= mx; ++i) {
    reverse(rem[i].begin(), rem[i].end());
    process(rem[i], -1);
  }
  int start = 1;
  for (int i = 1; i <= q; ++i) {
    start += dif[i];
    cout << start << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(15);
  int t = 1;
  while (t--) solve();
  42;
}

### Prompt

Please formulate a CPP solution to 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;
const int inf_int = 1e9 + 100;
const long long inf_ll = 1e18;
const double pi = 3.1415926535898;
template <class T1, class T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) {
  out << "( " << rhs.first << " , " << rhs.second << " )";
  return out;
}
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A>
string to_string(vector<vector<A>> v) {
  bool first = true;
  string res = "\n{";
  for (const auto &x : v) {
    if (!first) {
      res += ",\n ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}\n";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
bool debug = 0;
const int MAXN = 2e6 + 100;
const int LOG = 20;
const int mod = 1e9 + 7;
const int MX = (1e7 + 100);
struct event {
  int x, y;
  int L;
};
vector<event> add[MAXN];
vector<event> rem[MAXN];
int a[300][300];
int n, m;
inline int get_id(int x, int y) { return x * m + y; }
int parent[MAXN];
int get_parent(int v) {
  if (parent[v] == v) return v;
  return parent[v] = get_parent(parent[v]);
}
bool union_set(int a, int b) {
  a = get_parent(a);
  b = get_parent(b);
  if (a != b) {
    parent[a] = b;
    return true;
  }
  return false;
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
inline bool is_ok(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != -1;
}
int dif[MAXN];
void process(vector<event> &L, int coef) {
  if (L.empty()) return;
  for (int i = 0; i < n; ++i) {
    for (int e = 0; e < m; ++e) {
      a[i][e] = -1;
    }
  }
  for (int i = 0; i < n * m + 10; ++i) {
    parent[i] = i;
  }
  for (auto cur : L) {
    int x = cur.x, y = cur.y;
    int comp = 1;
    a[x][y] = 1;
    int v1 = get_id(x, y);
    for (int j = 0; j < 4; ++j) {
      int x1 = x + dx[j];
      int y1 = y + dy[j];
      int v2 = get_id(x1, y1);
      if (is_ok(x1, y1)) {
        if (union_set(v1, v2)) {
          comp--;
        }
      }
    }
    dif[cur.L] += comp * coef;
  }
}
void solve() {
  cin >> n >> m;
  int q;
  cin >> q;
  int mx = 0;
  for (int i = 0; i < q; ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    if (a[x][y] == c) {
      continue;
    }
    mx = max(mx, c);
    event temp = {x, y, i + 1};
    rem[a[x][y]].emplace_back(temp);
    a[x][y] = c;
    add[c].emplace_back(temp);
  }
  for (int i = 0; i < n; ++i) {
    for (int e = 0; e < m; ++e) {
      event temp = {i, e, q + 1};
      rem[a[i][e]].emplace_back(temp);
    }
  }
  for (int i = 0; i <= mx; ++i) {
    process(add[i], +1);
  }
  for (int i = 0; i <= mx; ++i) {
    reverse(rem[i].begin(), rem[i].end());
    process(rem[i], -1);
  }
  int start = 1;
  for (int i = 1; i <= q; ++i) {
    start += dif[i];
    cout << start << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(15);
  int t = 1;
  while (t--) solve();
  42;
}

```