#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long MOD = 777777777;
const long long INF = 1e18;
const long long N = 77777 + 10;
int seg[4 * N][4][4];
int a[N];
bool isval[4][4];
vector<pair<int, int>> valid;
void update(int id, int lq, int rq, int x, int l, int r) {
  if (lq >= r || rq <= l) return;
  if (lq <= l && r <= rq) {
    a[lq] = x;
    memset(seg[id], 0, sizeof seg[id]);
    if (a[lq] == 0) {
      for (int i = 1; i <= 3; i++) {
        seg[id][i][i] = 1;
      }
    } else {
      seg[id][a[lq]][a[lq]] = 1;
    }
    return;
  }
  long long mid = ((l + r) >> 1);
  update(id << 1, lq, rq, x, l, mid);
  update(id << 1 | 1, lq, rq, x, mid, r);
  memset(seg[id], 0, sizeof seg[id]);
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      for (pair<int, int> u : valid) {
        seg[id][i][j] =
            ((1ll * seg[id << 1][i][u.first] * seg[id << 1 | 1][u.second][j]) +
             seg[id][i][j]) %
            MOD;
      }
    }
  }
  return;
}
int n, q;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      cin >> isval[i][j];
      if (isval[i][j]) valid.push_back({i, j});
    }
  }
  for (int i = 1; i <= n; i++) {
    update(1, i, i + 1, 0, 1, n + 1);
  }
  for (int i = 0; i < q; i++) {
    long long ind, x;
    cin >> ind >> x;
    update(1, ind, ind + 1, x, 1, n + 1);
    long long ans = 0;
    for (int j = 1; j <= 3; j++) {
      for (int j1 = 1; j1 <= 3; j1++) {
        ans += seg[1][j][j1];
        ans %= MOD;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
