#include<bits/stdc++.h>
using namespace std;
main(){
int n,m;
cin>>n>>m;
int a[n][n];
int x=n*n;
vector<int> work;
int lin=0;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
cin>>a[i][j];
}
}
int t;
while(m--){
cin>>t;
if(t==1){
for(int j=0;j<n;++j){
for(int i=n-1;i>=0;--i){
int k=a[i][j];
work.push_back(k);
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
a[i][j]=work[lin];
++lin;
}
}
for(int i=0;i<x;++i){
cout<<work[i]<<" ";
if((i+1)%n==0)cout<<endl;
}
cout<<endl;
lin=0;
work.clear();
}
if(t==2){
for(int j=n-1;j>=0;--j){
for(int i=0;i<n;++i){
int k=a[i][j];
work.push_back(k);
//++lin;
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
a[i][j]=work[lin];
++lin;
}
}
for(int i=0;i<x;++i){
cout<<work[i]<<" ";
if((i+1)%n==0)cout<<endl;
}
cout<<endl;
lin=0;
work.clear();
}
if(t==3){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
work.push_back(a[i][j]);
//++lin;
}
}
lin=x-1;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
a[i][j]=work[lin];
--lin;
}
}
for(int i=x-1;i>=0;--i){
cout<<work[i]<<" ";
if(i%n==0)cout<<endl;
}
cout<<endl;
lin=0;
work.clear();
}
}
}