#include <bits/stdc++.h>
using namespace std;
int a[10001][10001],n,m,b,c,d,e,temp[10001][10001];
void rev(int sx,int sy,int ex,int ey){
for(int i = sx; i <= ex; i++){
for(int j = sy; j <= ey; j++){
temp[i][j] = a[ex + 1 - j][i];
}
}
for(int i = sx; i <= ex; i++){
for(int j = sy; j <= ey; j++){
a[i][j] = temp[i][j];
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
a[i][j] = (i - 1) * n + j;
}
}
for(int i = 1; i <= m; i++){
cin >> b >> c >> d >> e;
if(e){
rev(b - d,c - d,b + d,c + d);
rev(b - d,c - d,b + d,c + d);
rev(b - d,c - d,b + d,c + d);
}else{
rev(b - d,c - d,b + d,c + d);
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << a[i][j] << " ";
}
cout << "\n";
}
return 0;
}