#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
 
#define debug(x) #x << " = " << x << '\n'
using ll = long long;
 
template<class T> using vec = std::vector<T>; // Rebegus
 
/*
 
n, m
a11 a12 ... a1m
...
an1 an2 ... anm
q
x1 y1 c1
x2 y2 c2
...
xq yq cq
 
*/
 
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
 
  int n, m;
  std::cin >> n >> m;
 
  std::vector<std::vector<int>> a(n, std::vector<int>(m));
 
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      std::cin >> a[i][j];
      a[i][j]--;
    }
  }
 
  std::vector<bool> works(n * m, false);
  std::vector<std::set<std::pair<int, int>>> st(n * m);
 
  std::vector<std::vector<std::vector<int>>> cnt(n * m, std::vector<std::vector<int>>(3, std::vector<int>(3, 0)));
  
  auto refreshColor = [&](int c) {
    int cnt_total = (int) st[c].size();
    if (cnt_total == 0 || (cnt_total & 1)) {
      works[c] = false;
      return;
    } 
    if (n == 1) {
      works[c] = true;
      return;
    } 
    if (n == 2) {
      if (cnt[c][0][0] % 2 == 0) {
        works[c] = true;
      } else {
        works[c] = (cnt[c][0][1] > 0);
      }
      return;
    }
    if (cnt[c][0][0] % 2 == 0 && cnt[c][1][1] % 2 == 0) {
      works[c] = true;
      return;
    }
 
    if (cnt[c][0][0] % 2 == 0) {
      if (cnt[c][1][2] || (cnt[c][0][1] && cnt[c][0][2])) {
        works[c] = true;
      } else {
        works[c] = false;
      }
    } else if (cnt[c][1][1] % 2 == 0) {
      if (cnt[c][0][2] || (cnt[c][0][1] && cnt[c][1][2])) {
        works[c] = true;
      } else {
        works[c] = false;
      }
    } else {
      assert(cnt[c][2][2] % 2 == 0);
      if (cnt[c][0][1] || (cnt[c][0][2] && cnt[c][1][2])) {
        works[c] = true;
      } else {
        works[c] = false;
      }
    }
  };
 
  int answer = 0;
  auto upd = [&](int i, int j, int c, int add) {
    for (int z = 0; z < n; z++) {
      int x = z, y = i; 
      if (x > y) {
        std::swap(x, y);
      }
 
      if (st[c].count({z, j})) {
        cnt[c][x][y] += add;
      }
    }
  };
 
  auto update = [&](int i, int j, int c) {
    answer -= works[a[i][j]];
    upd(i, j, a[i][j], -1);
    st[a[i][j]].erase({i, j});
    refreshColor(a[i][j]);
    answer += works[a[i][j]];
    a[i][j] = c;
    answer -= works[a[i][j]];
    st[a[i][j]].insert({i, j});
    upd(i, j, a[i][j], +1);
    refreshColor(a[i][j]);
    answer += works[a[i][j]];
  };
  
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      st[a[i][j]].insert({i, j});
      upd(i, j, a[i][j], +1);
    }
  }
  
  for (int c = 0; c < n * m; c++) {
    refreshColor(c);
    answer += works[c];
  }
 
  
  int q;
  std::cin >> q;
 
  std::cout << answer << '\n';
  while (q--) {
    int x, y, c;
    std::cin >> x >> y >> c;
    x--, y--, c--;
    update(x, y, c);
    std::cout << answer << '\n';
  }
 
  return 0;
}
