#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long N;
  cin >> N;
  long long mat[N][N];
  long long k = 0;
  long long ch = 0;
  long long times = N / 2;
  long long cnt = 0;
  while (times--) {
    if (cnt % 2 == 0) {
      long long st = cnt * 2;
      long long end = (cnt + 1) * 2;
      for (long long i = 0; i < N; i++) {
        for (long long j = st; j < end; j++) {
          mat[i][j] = k++;
        }
      }
    } else {
      long long st = cnt * 2;
      long long end = (cnt + 1) * 2;
      for (long long i = N - 1; i >= 0; i--) {
        for (long long j = st; j < end; j++) {
          mat[i][j] = k++;
        }
      }
    }
    cnt++;
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      cout << mat[i][j] << " ";
    }
    cout << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.flush();
  long long t = 1;
  while (t--) {
    solve();
  }
}
