#include <bits/stdc++.h>
template <typename T>
void rd(T& x) {
  long long f = 0, c;
  while ((c = getchar()) < 48 || 57 < c) f ^= !(c ^ 45);
  x = (c & 15);
  while (47 < (c = getchar()) && c < 58) x = x * 10 + (c & 15);
  if (f) x = -x;
}
template <typename T>
inline void pt(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) pt(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void pt(T x, long long c) {
  pt(x), putchar(c);
}
template <typename T>
inline T max(const T& x, const T& y) {
  return x < y ? y : x;
}
template <typename T>
inline T min(const T& x, const T& y) {
  return x < y ? x : y;
}
const long long N = 1005;
long long n, a[N][N];
signed main() {
  rd(n);
  long long ind = 0;
  for (long long i = 1; i <= n; i += 4)
    for (long long j = 1; j <= n; j += 4) {
      for (long long x = (i); x <= (i + 3); ++x)
        for (long long y = (j); y <= (j + 3); ++y) a[x][y] = ind++;
    }
  for (long long i = (1); i <= (n); ++i)
    for (long long j = (1); j <= (n); ++j) pt(a[i][j], j == n ? '\n' : ' ');
  return 0;
}
