func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 305, Q = (int)2e6 + 50;   struct Qry {     int x, y, c;     void read() {       cin >> x >> y >> c;       x--, y--;     }   } qry[Q];   int n, m, q;   int mp[N][N];   int f[N * N];   int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};   int ch[Q], off[Q];   int ans[Q];   int gid(int i, int j) { return i * m + j; }   int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }   bool unite(int x, int y) {     x = find(x);     y = find(y);     if (x == y) return false;     f[x] = y;     return true;   }   int build(int val, int ig) {     for (int k = 0; k < n * m; k++) {       f[k] = k;     }     for (int ki = 0; ki < n; ki++) {       for (int kj = 0; kj < m; kj++) {         if (ig != -1 && (!((mp[ki][kj] == val) ^ ig))) continue;         if (ki + 1 < n && mp[ki + 1][kj] == mp[ki][kj])           unite(gid(ki + 1, kj), gid(ki, kj));         if (kj + 1 < m && mp[ki][kj + 1] == mp[ki][kj])           unite(gid(ki, kj + 1), gid(ki, kj));       }     }     int ccnt = 0;     for (int k = 0; k < n * m; k++) ccnt += (f[k] == k);     return ccnt;   }   bool in_bound(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> q;     for (int i = 0; i < q; i++) qry[i].read();     int r = 0;     for (int i = 0; i < q; i = r) {       while (r < q && qry[r].c == qry[i].c) r++;       build(qry[i].c, 0);       int cur = 0;       for (int j = i; j < r; j++) {         int x = qry[j].x, y = qry[j].y;         ch[j] = mp[x][y];         if (mp[x][y] != qry[j].c) cur++;         mp[x][y] = qry[j].c;         for (int d = 0; d < 4; d++) {           int nx = x + dx[d], ny = y + dy[d];           if (in_bound(nx, ny) && mp[x][y] == mp[nx][ny]) {             if (unite(gid(x, y), gid(nx, ny))) cur--;           }         }         off[j] += cur;       }       cur = 0;       build(qry[i].c, 1);       for (int j = r - 1; j >= i; j--) {         off[j] += cur;         int x = qry[j].x, y = qry[j].y;         if (mp[x][y] != ch[j]) {           cur++;           mp[x][y] = ch[j];           for (int d = 0; d < 4; d++) {             int nx = x + dx[d], ny = y + dy[d];             if (in_bound(nx, ny) && mp[x][y] == mp[nx][ny]) {               if (unite(gid(x, y), gid(nx, ny))) cur--;             }           }         }       }       int ccnt = build(qry[i].c, -1);       for (int j = i; j < r; j++) {         ans[j] = ccnt - cur + off[j];       }       for (int j = i; j < r; j++) {         int x = qry[j].x, y = qry[j].y;         mp[x][y] = qry[j].c;       }     }     for (int i = 0; i < q; i++) cout << ans[i] <<   n ;   }   