#include <iostream>
using namespace std;
const int N = 1e3 + 10;
int arr[N][N];
// 方向向量 - 右、左下、下、右上
int dx[] = {0, 1, 1, -1};
int dy[] = {1, -1, 0, 1};
int main() {
int n; cin >> n;
int cnt = 1; // 从 1 开始输出
int pos = 0; // 方向向量下标
int x = 1, y = 1; // 初始位置
int flag = 0, cn = 0; // cn 统计同一条线上爬过几个点 // flag 表示是否爬完了对角线,方便逆转 pos 更新方向
int f = 0; // 标记 右、下 只能走一次
while(cnt <= n * n) {
arr[x][y] = cnt;
cn++;
if(cn == n) flag = 1; // 爬完对角线要逆转 pos 方向了
int a = x + dx[pos], b = y + dy[pos];
if(f || a < 1 || a > n || b < 1 || b > n) {
!flag ? pos++ : pos--;
if(pos > 3) pos = 0;
else if(pos < 0) pos = 3;
cn = 1; // 转弯了要重新计数
f = 0;
}
if(pos == 0 || pos == 2) f = 1; // 下次记得转弯
x += dx[pos], y += dy[pos];
cnt++;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}