#include <bits/stdc++.h>
using namespace std;
const int MOD = 777777777;
const int MAXN = 80000;
int add(int a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
  return a;
}
int n, m;
bool is[4][4];
void read() {
  cin >> n >> m;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) cin >> is[i][j];
}
long long tr[MAXN * 4][4][4];
void merge(int v) {
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) tr[v][i][j] = 0;
  for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++)
      for (int p = 1; p <= 3; p++)
        for (int k = 1; k <= 3; k++)
          if (is[j][p])
            tr[v][i][k] =
                add(tr[v][i][k], tr[v * 2][i][j] * tr[v * 2 + 1][p][k] % MOD);
}
void build(int v, int l, int r) {
  if (l == r) {
    tr[v][1][1] = tr[v][2][2] = tr[v][3][3] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(v * 2, l, mid);
  build(v * 2 + 1, mid + 1, r);
  merge(v);
}
void update(int v, int l, int r, int pos, int val) {
  if (l == r) {
    if (val != 0) {
      tr[v][1][1] = tr[v][2][2] = tr[v][3][3] = 0;
      tr[v][val][val] = 1;
    } else
      tr[v][1][1] = tr[v][2][2] = tr[v][3][3] = 1;
    return;
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    update(v * 2, l, mid, pos, val);
  else
    update(v * 2 + 1, mid + 1, r, pos, val);
  merge(v);
}
void solve() {
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int pos, val;
    cin >> pos >> val;
    update(1, 1, n, pos, val);
    int ans = 0;
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++) ans = add(ans, tr[1][i][j]);
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  read();
  solve();
}
