#include <bits/stdc++.h>   using namespace std;   constexpr int kN = int(3E2 + 10), kC = int(2E6 + 10);   vector<pair<int, int>> v[kC], nv[kC];   int ans[kC], a[kN][kN], pos[kN][kN], p[kN * kN], up[kN * kN], dwn[kN * kN],       lft[kN * kN], rht[kN * kN], nans[kC];   bool have[kN * kN];   int Find(int n) { return p[n] == n ? n : p[n] = Find(p[n]); }   bool merge(int l, int r) {     l = Find(l), r = Find(r);     if (l == r)       return false;     else {       p[l] = r;       return true;     }   }   int main() {     int n, m, q, x, y, c, now = 0, tmp;     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) a[i][j] = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) pos[i][j] = now++;     for (int i = 1; i < n; i++)       for (int j = 1; j <= m; j++) {         up[pos[i][j]] = pos[i + 1][j];         dwn[pos[i + 1][j]] = pos[i][j];       }     for (int i = 1; i <= m; i++) up[pos[n][i]] = dwn[pos[1][i]] = -1;     for (int i = 1; i <= n; i++)       for (int j = 1; j < m; j++) {         lft[pos[i][j]] = pos[i][j + 1];         rht[pos[i][j + 1]] = pos[i][j];       }     for (int i = 1; i <= n; i++) lft[pos[i][m]] = rht[pos[i][1]] = -1;     for (int i = 1; i <= q; i++) {       scanf( %d%d%d , &x, &y, &c);       if (a[x][y] == c)         continue;       else {         nv[a[x][y]].push_back({pos[x][y], i});         a[x][y] = c;         v[a[x][y]].push_back({pos[x][y], i});       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) nv[a[i][j]].push_back({pos[i][j], q + 1});     ans[0] = nans[q + 1] = 0;     for (int i = 0; i <= c; i++) {       for (int j = 0; j < now; j++) p[j] = j;       for (int j = 0; j < now; j++) have[j] = false;       tmp = 0;       for (pair<int, int> j : v[i]) {         ans[j.second]++;         tmp++;         have[j.first] = true;         if (up[j.first] >= 0 && have[up[j.first]])           if (merge(j.first, up[j.first])) {             tmp--;             ans[j.second]--;           }         if (lft[j.first] >= 0 && have[lft[j.first]])           if (merge(j.first, lft[j.first])) {             ans[j.second]--;             tmp--;           }         if (rht[j.first] >= 0 && have[rht[j.first]])           if (merge(j.first, rht[j.first])) {             ans[j.second]--;             tmp--;           }         if (dwn[j.first] >= 0 && have[dwn[j.first]])           if (merge(j.first, dwn[j.first])) {             ans[j.second]--;             tmp--;           }       }       reverse(nv[i].begin(), nv[i].end());       if (tmp) {         ans[v[i].back().second + 1] -= tmp;         nans[v[i].back().second] -= tmp;       }       for (int j = 0; j < now; j++) p[j] = j;       for (int j = 0; j < now; j++) have[j] = false;       for (pair<int, int> j : nv[i]) {         nans[j.second - 1]++;         have[j.first] = true;         if (up[j.first] >= 0 && have[up[j.first]])           if (merge(j.first, up[j.first])) nans[j.second - 1]--;         if (lft[j.first] >= 0 && have[lft[j.first]])           if (merge(j.first, lft[j.first])) nans[j.second - 1]--;         if (rht[j.first] >= 0 && have[rht[j.first]])           if (merge(j.first, rht[j.first])) nans[j.second - 1]--;         if (dwn[j.first] >= 0 && have[dwn[j.first]])           if (merge(j.first, dwn[j.first])) nans[j.second - 1]--;       }     }     for (int i = q; i >= 1; i--) nans[i] += nans[i + 1];     for (int i = 1; i <= q; i++) ans[i] += ans[i - 1];     for (int i = 1; i <= q; i++) printf( %d n , ans[i] + nans[i]);   }