#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 17;
const long long MOD = 777777777;
bool lucky[10][10];
struct Matrix {
  long long a[3][3];
  Matrix() { fill(a[0], a[3], 0); }
};
Matrix comb(const Matrix& lhs, const Matrix& rhs) {
  Matrix ret;
  for (int j1 = 0; j1 < 3; ++j1) {
    for (int i2 = 0; i2 < 3; ++i2) {
      if (lucky[j1][i2]) {
        for (int i1 = 0; i1 < 3; ++i1) {
          for (int j2 = 0; j2 < 3; ++j2) {
            ret.a[i1][j2] =
                (ret.a[i1][j2] + lhs.a[i1][j1] * rhs.a[i2][j2]) % MOD;
          }
        }
      }
    }
  }
  return ret;
}
struct SegTree {
  int n;
  Matrix a[MAXN + MAXN];
  static int L(int i) { return i << 1; }
  static int R(int i) { return L(i) ^ 1; }
  void init(int m) {
    n = 1;
    while (n < m) {
      n <<= 1;
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 3; ++j) {
        a[n + i].a[j][j] = 1;
      }
    }
    for (int i = n - 1; i > 0; --i) {
      a[i] = comb(a[L(i)], a[R(i)]);
    }
  }
  void set(int i, int t) {
    i += n;
    for (int j = 0; j < 3; ++j) {
      a[i].a[j][j] = (t == -1 || t == j) ? 1 : 0;
    }
    while (i > 1) {
      i >>= 1;
      a[i] = comb(a[L(i)], a[R(i)]);
    }
  }
  Matrix query(int p, int pl, int pr, int l, int r) {
    if (pl == l && pr == r) {
      return a[p];
    } else {
      int pm = (pl + pr) / 2;
      if (r <= pm) {
        return query(L(p), pl, pm, l, r);
      } else if (pm <= l) {
        return query(R(p), pm, pr, l, r);
      } else {
        return comb(query(L(p), pl, pm, l, pm), query(R(p), pm, pr, pm, r));
      }
    }
  }
} st;
int main() {
  int n, m, v, t;
  Matrix ret;
  long long sum;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      scanf("%d", &t);
      lucky[i][j] = t > 0;
    }
  }
  st.init(n);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &v, &t);
    st.set(v - 1, t - 1);
    ret = st.query(1, 0, st.n, 0, n);
    sum = 0;
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        sum += ret.a[j][k];
      }
    }
    printf("%d\n", (int)(sum % MOD));
  }
  return 0;
}
