#include <bits/stdc++.h>
using namespace std;
const int N = 77777;
const long long MOD = 777777777;
struct Seg {
  int l, r, lc, rc;
  long long v[3][3];
} seg[N * 2];
int fix[N];
int segn;
bool good[3][3];
bool test(int loc, int v) { return fix[loc] & (1u << (v)); }
void update(Seg &ns, Seg &ls, Seg &rs) {
  memset(ns.v, 0, sizeof(ns.v));
  for (int n_ = (3), li = 0; li < n_; ++li)
    if (test(ls.l, li))
      for (int n_ = (3), lj = 0; lj < n_; ++lj)
        if (test(ls.r, lj))
          for (int n_ = (3), ri = 0; ri < n_; ++ri)
            if (test(rs.l, ri))
              for (int n_ = (3), rj = 0; rj < n_; ++rj)
                if (test(rs.r, rj))
                  if (good[lj][ri]) {
                    ns.v[li][rj] += ls.v[li][lj] * rs.v[ri][rj];
                    ns.v[li][rj] %= MOD;
                  }
}
int seg_init(int l, int r) {
  int p = segn++;
  Seg &ns = seg[p];
  ns.l = l, ns.r = r;
  int mid = (l + r) / 2;
  if (l == r) {
    memset(ns.v, 0, sizeof(ns.v));
    for (int n_ = (3), i = 0; i < n_; ++i) ns.v[i][i] = 1;
    return p;
  }
  ns.lc = seg_init(l, mid);
  ns.rc = seg_init(mid + 1, r);
  update(ns, seg[ns.lc], seg[ns.rc]);
  return p;
}
void seg_ins(int p, int v, int t) {
  Seg &ns = seg[p];
  if (ns.l == ns.r) {
    memset(ns.v, 0, sizeof(ns.v));
    if (t == -1) {
      for (int n_ = (3), i = 0; i < n_; ++i) ns.v[i][i] = 1;
      fix[v] = t;
    } else {
      ns.v[t][t] = 1;
      fix[v] = (1u << (t));
    }
    return;
  }
  int mid = (ns.l + ns.r) / 2;
  if (v <= mid)
    seg_ins(ns.lc, v, t);
  else
    seg_ins(ns.rc, v, t);
  update(ns, seg[ns.lc], seg[ns.rc]);
}
int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  for (int n_ = (3), i = 0; i < n_; ++i)
    for (int n_ = (3), j = 0; j < n_; ++j) cin >> good[i][j];
  memset(fix, -1, sizeof(fix));
  seg_init(0, n - 1);
  while (m-- > 0) {
    int v, t;
    cin >> v >> t;
    v--, t--;
    seg_ins(0, v, t);
    long long ans = 0;
    for (int n_ = (3), i = 0; i < n_; ++i)
      if (test(0, i))
        for (int n_ = (3), j = 0; j < n_; ++j)
          if (test(n - 1, j)) {
            ans += seg[0].v[i][j];
          }
    ans %= MOD;
    cout << ans << endl;
  }
  return 0;
}
