#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 5;
long long n, k, a[maxn], max_m;
map<long long, long long> mp;

long long mul(long long x, long long p) {
    long long ans = 1;
    while(p) {
        if(p & 1) ans *= x;
        x *= x;
        p >>= 1;
    }
    return ans;
}

long long useful(long long tn) {
    if(tn == 1) return mp[1] - 1;
    long long ans = 0, ex = 1, tmp = tn;
    for(long long i = 2; tmp != 1; i++) {
        int num = 0;
        while(tmp % i == 0) {
            num++;
            tmp /= i;
        }
        if(num % k) ex *= mul(i, k-(num%k));
    }
    long long key = tn * ex;
    for(long long i = key; i <= max_m; i += key) {
        long long ri = i / tn;
        if(!mp.count(ri)) continue;
        ans += mp[ri] - (ri == tn);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    max_m = a[n-2] * a[n-1];
    mp[a[0]] = 1;
    for(int i = 1; i < n; i++) {
        if(a[i] == a[i-1]) mp[a[i]]++;
        else mp[a[i]] = 1;
    }
    long long ans = 0, laans = 0;
    for(int i = 0; i < n; i++) ans += useful(a[i]);
    cout << ans / 2 << endl;
    return 0;
}


#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 5;
long long n, k, a[maxn], max_m;
map<long long, long long> mp;

long long mul(long long x, long long p) {
    long long ans = 1;
    while(p) {
        if(p & 1) ans *= x;
        x *= x;
        p >>= 1;
    }
    return ans;
}

long long useful(long long tn) {
    if(tn == 1) return mp[1] - 1;
    long long ans = 0, ex = 1, tmp = tn;
    for(long long i = 2; tmp != 1; i++) {
        int num = 0;
        while(tmp % i == 0) {
            num++;
            tmp /= i;
        }
        if(num % k) ex *= mul(i, k-(num%k));
    }
    long long key = tn * ex;
    for(long long i = key; i < max_m; i += key) {
        long long ri = i / tn;
        if(!mp.count(ri)) continue;
        ans += mp[ri] - (ri == tn);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    max_m = a[n-2] * a[n-1];
    mp[a[0]] = 1;
    for(int i = 1; i < n; i++) {
        if(a[i] == a[i-1]) mp[a[i]]++;
        else mp[a[i]] = 1;
    }
    long long ans = 0, laans = 0;
    for(int i = 0; i < n; i++) {
        if(!i || a[i] != a[i-1]) {
            laans = useful(a[i]);
        }
        ans += laans;
    }
    cout << ans / 2 << endl;
    return 0;
}


#include<cmath>
#include<unordered_map>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 5;
long long n, k, a[maxn];
unordered_map<long long, int> mp;

long long mul(long long x, long long p) {
    long long ans = 1;
    while(p) {
        if(p & 1) ans *= x;
        x *= x;
        p >>= 1;
    }
    return ans;
}

long long useful() {
    long long ans = 0, ex, pi, tmp, en;
    for(int i = 0; i < n; i++) {
        if(!i || a[i] != a[i-1]) {
            ex = 1, tmp = a[i], pi = 1, en = sqrt(tmp) + 1;
            for(long long i = 2; tmp != 1 && i < en; i++) {
                int num = 0;
                while(tmp % i == 0) {
                    num++;
                    tmp /= i;
                }
                num %= k;
                if(num) {
                    pi *= mul(i, num);
                    ex *= mul(i, k-num);
                }
            }
        }
        if(tmp != 1) {
            pi *= tmp;
            ex *= mul(tmp, k-1);
        }
        if(mp.count(ex)) ans += mp[ex];
        if(mp.count(pi)) mp[pi]++;
        else mp[pi] = 1;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    cout << useful() << endl;
    return 0;
}


#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 5;
long long n, k, a[maxn];
vector<long long> vec;
map<long long, long long> mp;

long long getmul(long long i) {
    long long ans = i;
    for(int p = 1; p < k; p++) ans *= i;
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    mp[a[0]] = 1;
    for(int i = 1; i < n; i++) {
        if(a[i] == a[i-1]) mp[a[i]]++;
        else mp[a[i]] = 1;
    }
    long long max_m = a[n-1] * a[n-2];
    for(long long i = 1; ; i++) {
        long long mul = getmul(i);
        if(mul > max_m) break;
        vec.push_back(mul);
    }
    int si = vec.size()-1;
    long long ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = si; j >= 0; j--) {
            if(vec[j] < a[i]) break;
            if(vec[j] % a[i]) continue;
            long long tk = vec[j] / a[i];
            if(!mp.count(tk)) continue;
            long long p = mp[tk] - (tk == a[i]);
            ans += p;
        }
    }
    cout << ans / 2 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int mark[N];
vector<int> prime;
map< vector< pair<int, int> > , int > mp;
vector< pair<int, int> > fac[N];
int n, k;
int ara[N];

void seive(int n)
{
    int i, j, rt = sqrt(n);
    prime.push_back(2);
    for (i = 3; i <= n; i += 2)
    {
        if (mark[i] == 0)
        {
            prime.push_back(i);
            if (i <= rt)
            {
                for (j = i * i; j <= n; j += 2 * i)
                    mark[j] = 1;
            }
        }
    }
}

void udpadok(int n, int at)
{
    int i, j, rt = sqrt(n), c;
    for (i = 0; i < prime.size( ) && prime[i] <= rt; i++)
    {
        if (n % prime[i] == 0)
        {
            c = 0;
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                c++;
            }
            c = c % k;
            if (c != 0) fac[at].push_back(make_pair(prime[i], c));
            rt = sqrt(n);
        }
    }
    if (n != 1)
        fac[at].push_back(make_pair(n, 1));
}

int main( )
{
    seive(100000);
    int i, j, l, c = 0, r, rt, p;
    long long int s = 0;
    vector< pair<int, int> > v;
    cin >> n >> k;
    for (i = 1; i <= n; i++)
    {
        cin >> ara[i];
        udpadok(ara[i], i);
    }
    for (i = 1; i <= n; i++)
    {
        v.clear( );
        for (j = 0; j < fac[i].size( ); j++)
        {
            p = fac[i][j].first;
            c = k - fac[i][j].second;
            v.push_back(make_pair(p, c));
        }
        s += mp[v];
        mp[fac[i]]++;
    }
    cout << s << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int mark[N];
vector<int> prime;
map<int, int> mp;
int n, k;
int ara[N];

void seive(int n)
{
    int i, j, rt = sqrt(n);
    prime.push_back(2);
    for (i = 3; i <= n; i += 2)
    {
        if (mark[i] == 0)
        {
            prime.push_back(i);
            if (i <= rt)
            {
                for (j = i * i; j <= n; j += 2 * i)
                    mark[j] = 1;
            }
        }
    }
}

int bigmod(int a, int b)
{
    if (b == 0) return 1;
    int x = bigmod(a, b / 2);
    x = x * x;
    if (b % 2 == 1) x = x * a;
    return x;
}

int udpadok(int n)
{
    int i, j, rt = sqrt(n), c = 0, l = 1;
    for (i = 0; i < prime.size( ) && prime[i] <= rt; i++)
    {
        if (n % prime[i] == 0)
        {
            c = 0;
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                c++;
            }
            c = c % k;
            l *= bigmod(prime[i], c);
            rt = sqrt(n);
        }
    }
    if (n != 1)
        l = l * n;
    return l;
}

int rev_udpadok(int n)
{
    int i, j, rt = sqrt(n), c = 0, l = 1;
    for (i = 0; i < prime.size( ) && prime[i] <= rt; i++)
    {
        if (n % prime[i] == 0)
        {
            c = 0;
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                c++;
            }
            c = (k - c) % k;
            l *= bigmod(prime[i], c);
            rt = sqrt(n);
        }
    }
    if (n != 1)
    {
        c = (k - 1) % k;
        l *= bigmod(prime[i], c);
    }
    return l;
}

int main( )
{
    seive(100000);
    int i, j, l, c = 0;
    cin >> n >> k;
    for (i = 1; i <= n; i++)
        cin >> ara[i];
    for (i = 1; i <= n; i++)
    {
        l = rev_udpadok(ara[i]);
        //cout << l << "\n";
        c += mp[l];
        l = udpadok(ara[i]);
        //cout << ara[i] << " " << l << " " << c << "\n";
        mp[l]++;
    }
    cout << c << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int mark[N];
vector<int> prime;
map<int, int> mp;
int n, k;
int ara[N];

void seive(int n)
{
    int i, j, rt = sqrt(n);
    prime.push_back(2);
    for (i = 3; i <= n; i += 2)
    {
        if (mark[i] == 0)
        {
            prime.push_back(i);
            if (i <= rt)
            {
                for (j = i * i; j <= n; j += 2 * i)
                    mark[j] = 1;
            }
        }
    }
}

int bigmod(int a, int b)
{
    if (b == 0) return 1;
    int x = bigmod(a, b / 2);
    x = x * x;
    if (b % 2 == 1) x = x * a;
    return x;
}

int udpadok(int n)
{
    int i, j, rt = sqrt(n), c = 0, l = 1;
    for (i = 0; i < prime.size( ) && prime[i] <= rt; i++)
    {
        if (n % prime[i] == 0)
        {
            c = 0;
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                c++;
            }
            c = c % k;
            l *= bigmod(prime[i], c);
            rt = sqrt(n);
        }
    }
    if (n != 1)
        l = l * n;
    return l;
}

int rev_udpadok(int n)
{
    int i, j, rt = sqrt(n), c = 0, l = 1;
    for (i = 0; i < prime.size( ) && prime[i] <= rt; i++)
    {
        if (n % prime[i] == 0)
        {
            c = 0;
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                c++;
            }
            c = (k - c) % k;
            for (j = 0; j < c; j++)
            {
                l = l * prime[i];
                if (l > 1e9)
                    return 0;
            }
            rt = sqrt(n);
        }
    }
    if (n != 1)
    {
        c = (k - 1) % k;
        for (j = 0; j < c; j++)
        {
            l = l * prime[i];
            if (l > 1e9)
                return 0;
        }
    }
    return l;
}

int main( )
{
    seive(100000);
    int i, j, l, c = 0;
    cin >> n >> k;
    for (i = 1; i <= n; i++)
        cin >> ara[i];
    for (i = 1; i <= n; i++)
    {
        l = rev_udpadok(ara[i]);
        //cout << l << "\n";
        c += mp[l];
        l = udpadok(ara[i]);
        //cout << ara[i] << " " << l << " " << c << "\n";
        mp[l]++;
    }
    cout << c << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int mark[N];
vector<int> prime;
map< vector< pair<int, int> > , int > mp;
vector< pair<int, int> > fac[N];
int n, k;
int ara[N];

void seive(int n)
{
    int i, j, rt = sqrt(n);
    prime.push_back(2);
    for (i = 3; i <= n; i += 2)
    {
        if (mark[i] == 0)
        {
            prime.push_back(i);
            if (i <= rt)
            {
                for (j = i * i; j <= n; j += 2 * i)
                    mark[j] = 1;
            }
        }
    }
}

void udpadok(int n, int at)
{
    int i, j, rt = sqrt(n), c;
    for (i = 0; i < prime.size( ) && prime[i] <= rt; i++)
    {
        if (n % prime[i] == 0)
        {
            c = 0;
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                c++;
            }
            c = c % k;
            if (c != 0) fac[at].push_back(make_pair(prime[i], c));
            rt = sqrt(n);
        }
    }
    if (n != 1)
        fac[at].push_back(make_pair(n, 1));
}

int main( )
{
    seive(100000);
    int i, j, l, c = 0, s = 0, r, rt, p;
    vector< pair<int, int> > v;
    cin >> n >> k;
    for (i = 1; i <= n; i++)
    {
        cin >> ara[i];
        udpadok(ara[i], i);
    }
    for (i = 1; i <= n; i++)
    {
        v.clear( );
        for (j = 0; j < fac[i].size( ); j++)
        {
            p = fac[i][j].first;
            c = k - fac[i][j].second;
            v.push_back(make_pair(p, c));
        }
        s += mp[v];
        mp[fac[i]]++;
    }
    cout << s << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define umin(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;
const ll inf=1e10+5;
const int maxn=1e5+5;

int n,k,a[maxn],c[maxn],flag;
int fac[maxn],cnt[maxn],tot;
ll x2;
ll ans;

void dfs(ll now,int cur){
    if(cur==tot){
        ll x=now;
        ll y=x2/now;
        if(x<maxn && y<maxn){
            if(x<y){
                ans+=1LL*c[x]*c[y];
            }
            else if(flag && x==y){
                ans+=1LL*(c[x]-1)*c[x]/2;
                flag=0;
            }
        }
        return;
    }
    ll x=1;
    for(int i=0;i<=cnt[cur];++i){
        dfs(now*x,cur+1);
        x*=fac[cur];
    }
}

void solve2(){
    ans=1LL*c[1]*(c[1]-1)/2;
    for(int x=2;x<=100000;++x){
        int y=x;
        int m=sqrt(y)+0.5;
        tot=0;
        for(int i=2;i<=m;++i){
            if(y%i==0){
                fac[tot]=i;
                cnt[tot]=0;
                while(y%i==0){
                    ++cnt[tot];
                    y/=i;
                }
                ++tot;
            }
        }
        if(y>1){
            fac[tot]=y;
            cnt[tot++]=1;
        }
        for(int i=0;i<tot;++i) cnt[i]*=2;
        x2=1LL*x*x;
        flag=1;
        dfs(1LL,0);
    }
    printf("%I64d\n",ans);
}

ll qpow(ll x,ll y){
    ll res=1;
    while(y--){
        res*=x;
        if(res>1e10+5)return 1e10+6;
    }
    return res;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i){
        scanf("%d",&a[i]);
        ++c[a[i]];
    }
    if(k==2){
        solve2();
        return 0;
    }
    if(k>=3){
        sort(a,a+n);
        for(int i=1;qpow(i,k)<=a[n-1]*a[n-1];i++){
            int tmp=qpow(i,k);
            for(int j=0;j<n;j++){
                if(tmp%a[j])continue;
                int t1=lower_bound(a+j+1,a+n,tmp/a[j])-a;
                int t2=upper_bound(a+j+1,a+n,tmp/a[j])-a;
                ans+=t2-t1;
            }
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define umin(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;
const ll inf=1e10+5;
const int maxn=1e5+5;

int n,k,a[maxn],c[maxn],flag;
int fac[maxn],cnt[maxn],tot;
ll x2;
ll ans;

void dfs(ll now,int cur){
    if(cur==tot){
        ll x=now;
        ll y=x2/now;
        if(x<maxn && y<maxn){
            if(x<y){
                ans+=1LL*c[x]*c[y];
            }
            else if(flag && x==y){
                ans+=1LL*(c[x]-1)*c[x]/2;
                flag=0;
            }
        }
        return;
    }
    ll x=1;
    for(int i=0;i<=cnt[cur];++i){
        dfs(now*x,cur+1);
        x*=fac[cur];
    }
}

void solve2(){
    ans=1LL*c[1]*(c[1]-1)/2;
    for(int x=2;x<=100000;++x){
        int y=x;
        int m=sqrt(y)+0.5;
        tot=0;
        for(int i=2;i<=m;++i){
            if(y%i==0){
                fac[tot]=i;
                cnt[tot]=0;
                while(y%i==0){
                    ++cnt[tot];
                    y/=i;
                }
                ++tot;
            }
        }
        if(y>1){
            fac[tot]=y;
            cnt[tot++]=1;
        }
        for(int i=0;i<tot;++i) cnt[i]*=2;
        x2=1LL*x*x;
        flag=1;
        dfs(1LL,0);
    }
    printf("%I64d\n",ans);
}

ll qpow(ll x,ll y){
    ll res=1;
    while(y--){
        res*=x;
        if(res>1e10+5)return 1e10+6;
    }
    return res;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i){
        scanf("%d",&a[i]);
        ++c[a[i]];
    }
    if(k==2){
        solve2();
        return 0;
    }
    if(k>=3){
        sort(a,a+n);
        for(int i=1;qpow(i,k)<=1LL*a[n-1]*a[n-1];i++){
            int tmp=qpow(i,k);
            for(int j=0;j<n;j++){
                if(tmp%a[j])continue;
                int t1=lower_bound(a+j+1,a+n,tmp/a[j])-a;
                int t2=upper_bound(a+j+1,a+n,tmp/a[j])-a;
                ans+=t2-t1;
            }
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define umin(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;
const ll inf=1e10+5;
const int maxn=1e5+5;

int n,k,a[maxn],c[maxn],flag;
int fac[maxn],cnt[maxn],tot;
ll x2;
ll ans;

void dfs(ll now,int cur){
    if(cur==tot){
        ll x=now;
        ll y=x2/now;
        if(x<maxn && y<maxn){
            if(x<y){
                ans+=1LL*c[x]*c[y];
            }
            else if(flag && x==y){
                ans+=1LL*(c[x]-1)*c[x]/2;
                flag=0;
            }
        }
        return;
    }
    ll x=1;
    for(int i=0;i<=cnt[cur];++i){
        dfs(now*x,cur+1);
        x*=fac[cur];
    }
}

void solve2(){
    ans=1LL*c[1]*(c[1]-1)/2;
    for(int x=2;x<=4;++x){
        int y=x;
        int m=sqrt(y)+0.5;
        tot=0;
        for(int i=2;i<=m;++i){
            if(y%i==0){
                fac[tot]=i;
                cnt[tot]=0;
                while(y%i==0){
                    ++cnt[tot];
                    y/=i;
                }
                ++tot;
            }
        }
        if(y>1){
            fac[tot]=y;
            cnt[tot++]=1;
        }
        for(int i=0;i<tot;++i) cnt[i]*=2;
        x2=1LL*x*x;
        flag=1;
        dfs(1LL,0);
    }
    printf("%I64d\n",ans);
}

ll qpow(ll x,ll y){
    ll res=1;
    while(y--){
        res*=x;
        if(res>1e10+5)return 1e10+6;
    }
    return res;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i){
        scanf("%d",&a[i]);
        ++c[a[i]];
    }
    if(k==2){
        solve2();
        return 0;
    }
    if(k>=3){
        sort(a,a+n);
        for(int i=1;qpow(i,k)<=a[n-1]*a[n-1];i++){
            int tmp=qpow(i,k);
            for(int j=0;j<n;j++){
                if(tmp%a[j])continue;
                int t1=lower_bound(a+j+1,a+n,tmp/a[j])-a;
                int t2=upper_bound(a+j+1,a+n,tmp/a[j])-a;
                ans+=t2-t1;
            }
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define umin(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;
const ll inf=1e10+5;
const int maxn=1e5+5;

int n,k,a[maxn],c[maxn],flag;
int fac[maxn],cnt[maxn],tot;
ll x2;
ll ans;

void dfs(ll now,int cur){
    if(cur==tot){
        ll x=now;
        ll y=x2/now;
        if(x<maxn && y<maxn){
            if(x<y){
                ans+=1LL*c[x]*c[y];
            }
            else if(flag && x==y){
                ans+=1LL*(c[x]-1)*c[x]/2;
                flag=0;
            }
        }
        return;
    }
    ll x=1;
    for(int i=0;i<=cnt[cur];++i){
        dfs(now*x,cur+1);
        x*=fac[cur];
    }
}

void solve2(){
    ans=1LL*c[1]*(c[1]-1)/2;
    for(int x=2;x<=100000;++x){
        int y=x;
        int m=sqrt(y)+0.5;
        tot=0;
        for(int i=2;i<=m;++i){
            if(y%i==0){
                fac[tot]=i;
                cnt[tot]=0;
                while(y%i==0){
                    ++cnt[tot];
                    y/=i;
                }
                ++tot;
            }
        }
        if(y>1){
            fac[tot]=y;
            cnt[tot++]=1;
        }
        for(int i=0;i<tot;++i) cnt[i]*=2;
        x2=1LL*x*x;
        flag=1;
        dfs(1LL,0);
    }
    printf("%I64d\n",ans);
}

ll qpow(ll x,ll y){
    ll res=1;
    while(y--){
        res*=x;
        if(res>1e10+5)return 1e10+6;
    }
    return res;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i){
        scanf("%d",&a[i]);
        ++c[a[i]];
    }
    if(k==2){
        solve2();
        return 0;
    }
    if(k>=3){
        sort(a,a+n);
        for(int i=1;qpow(i,k)<=1LL*a[n-1]*a[n-1];i++){
            ll tmp=qpow(i,k);
            for(int j=0;j<n;j++){
                if(tmp%a[j])continue;
                int t1=lower_bound(a+j+1,a+n,tmp/a[j])-a;
                int t2=upper_bound(a+j+1,a+n,tmp/a[j])-a;
                ans+=t2-t1;
            }
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<map>
using namespace std;
#define il inline
#define re register
#define debug printf("Now is Line : %d\n",__LINE__)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define int long long
#define D double
#define inf 123456789
#define mod 1000000007
il int read() {
    re int x = 0, f = 1; re char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
#define rep(i, s, t) for(re int i = s; i <= t; ++ i)
#define drep(i, s, t) for(re int i = t; i >= s; -- i)
#define Next(i, u) for(re int i = head[u]; i; i = e[i].next)
#define mem(k, p) memset(k, p, sizeof(k))
#define lb(x) (x)&(-(x))
#define ls k * 2
#define rs k * 2 + 1
#define maxn 1000005
int n, k, a[maxn], b[maxn], ans;
il int qpow(int a, int b) {
	int r = 1;
	while(b) {
		if(b & 1) r = a * r;
		a = a * a, b >>= 1;
	}
	return r;
}
map<int, int>q;
il void solve(int id, int x) {
	int pax1 = 1, pax2 = 1;
	for(re int i = 2; i * i <= x; ++ i) {
		if(x % i) continue;
		int now = 0;
		while(x % i == 0) ++ now, x /= i;
		pax1 *= qpow(i, (k - now) % k), pax2 *= qpow(i, now % k);
	}
	if(x != 1) pax1 *= qpow(x, k - 1), pax2 *= x;
	a[id] = pax1, b[id] = pax2;
}
signed main() {
	n = read(), k = read();
	rep(i, 1, n) a[i] = read(), solve(i, a[i]), ans += q[a[i]], ++ q[b[i]];
	printf("%I64d", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<map>
using namespace std;
#define il inline
#define re register
#define debug printf("Now is Line : %d\n",__LINE__)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define int long long
#define D double
#define inf 123456789
#define mod 1000000007
il int read() {
    re int x = 0, f = 1; re char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
#define rep(i, s, t) for(re int i = s; i <= t; ++ i)
#define drep(i, s, t) for(re int i = t; i >= s; -- i)
#define Next(i, u) for(re int i = head[u]; i; i = e[i].next)
#define mem(k, p) memset(k, p, sizeof(k))
#define lb(x) (x)&(-(x))
#define ls k * 2
#define rs k * 2 + 1
#define maxn 1000005
int n, k, a[maxn], b[maxn], ans, Max = 100000, flag;
il int mul(int a, int b) {
	a *= b;
	if(a > Max) flag = 1;
	return a;
}
il int qpow(int a, int b) {
	int r = 1;
	while(b) {
		if(b & 1) r = mul(a, r);
		a = mul(a, a), b >>= 1;
	}
	return r;
}
map<int, int>q;
il void solve(int id, int x) {
	int pax1 = 1, pax2 = 1;
	for(re int i = 2; i * i <= x; ++ i) {
		if(x % i) continue;
		int now = 0;
		while(x % i == 0) ++ now, x /= i;
		pax1 = mul(pax1, qpow(i, (k - now) % k));
		pax2 = mul(pax2, qpow(i, now % k));
	}
	if(x != 1) pax1 = mul(pax1, qpow(x, k - 1)), pax2 = mul(pax2, x);
	a[id] = pax1, b[id] = pax2;
}
signed main() {
	n = read(), k = read();
	rep(i, 1, n) a[i] = read();
	rep(i, 1, n) {
		flag = 0, solve(i, a[i]);
		if(flag == 1) continue;
		ans += q[a[i]], ++ q[b[i]];
	}
	printf("%I64d", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<map>
using namespace std;
#define il inline
#define re register
#define debug printf("Now is Line : %d\n",__LINE__)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define int long long
#define D double
#define inf 123456789
#define mod 1000000007
il int read() {
    re int x = 0, f = 1; re char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
#define rep(i, s, t) for(re int i = s; i <= t; ++ i)
#define drep(i, s, t) for(re int i = t; i >= s; -- i)
#define Next(i, u) for(re int i = head[u]; i; i = e[i].next)
#define mem(k, p) memset(k, p, sizeof(k))
#define lb(x) (x)&(-(x))
#define ls k * 2
#define rs k * 2 + 1
#define maxn 1000005
int n, k, a[maxn], b[maxn], ans, Max = 100000, flag;
il int mul(int a, int b) {
	a *= b;
	if(a > Max) flag = 1;
	return a;
}
il int qpow(int a, int b) {
	int r = 1;
	while(b) {
		if(b & 1) r = mul(a, r);
		a = a * a, b >>= 1;
		if(k && a > Max) flag = 1;
	}
	return r;
}
map<int, int>q;
il void solve(int id, int x) {
	int pax1 = 1, pax2 = 1;
	for(re int i = 2; i * i <= x; ++ i) {
		if(x % i) continue;
		int now = 0;
		while(x % i == 0) ++ now, x /= i;
		pax1 = mul(pax1, qpow(i, (k - now % k) % k));
		pax2 = mul(pax2, qpow(i, now % k));
	}
	if(x != 1) pax1 = mul(pax1, qpow(x, k - 1)), pax2 = mul(pax2, x);
	a[id] = pax1, b[id] = pax2;
}
signed main() {
	n = read(), k = read();
	rep(i, 1, n) a[i] = read();
	rep(i, 1, n) {
		flag = 0, solve(i, a[i]);
		if(flag == 1) continue;
		ans += q[a[i]], ++ q[b[i]];
	}
	printf("%I64d", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<map>
using namespace std;
#define il inline
#define re register
#define debug printf("Now is Line : %d\n",__LINE__)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define int long long
#define D double
#define inf 123456789
#define mod 1000000007
il int read() {
    re int x = 0, f = 1; re char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
#define rep(i, s, t) for(re int i = s; i <= t; ++ i)
#define drep(i, s, t) for(re int i = t; i >= s; -- i)
#define Next(i, u) for(re int i = head[u]; i; i = e[i].next)
#define mem(k, p) memset(k, p, sizeof(k))
#define lb(x) (x)&(-(x))
#define ls k * 2
#define rs k * 2 + 1
#define maxn 1000005
int n, k, a[maxn], b[maxn], ans, Max = 100000, flag;
il int mul(int a, int b) {
	a *= b;
	if(a > Max) flag = 1;
	return a;
}
il int qpow(int a, int b) {
	int r = 1;
	while(b) {
		if(b & 1) r = mul(a, r);
		a = a * a, b >>= 1;
		if(b && a > Max) flag = 1;
	}
	return r;
}
map<int, int>q;
il void solve(int id, int x) {
	int pax1 = 1, pax2 = 1;
	for(re int i = 2; i * i <= x; ++ i) {
		if(x % i) continue;
		int now = 0;
		while(x % i == 0) ++ now, x /= i;
		pax1 = mul(pax1, qpow(i, (k - now % k) % k));
		pax2 = mul(pax2, qpow(i, now % k));
	}
	if(x != 1) pax1 = mul(pax1, qpow(x, k - 1)), pax2 = mul(pax2, x);
	a[id] = pax1, b[id] = pax2;
}
signed main() {
	n = read(), k = read();
	rep(i, 1, n) a[i] = read();
	rep(i, 1, n) {
		flag = 0, solve(i, a[i]);
		if(flag == 1) continue;
		ans += q[a[i]], ++ q[b[i]];
	}
	printf("%I64d", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5, B = 316;

int isp[N], prime[N], pre[N];
int n, k, a, l[N], cnt[N], tot;
map<int, int> t;
map<map<int, int>, int> T;
long long ans;

void prepa() {
	for (int i = 2; i < N; i++) {
		if (!isp[i]) prime[++tot] = i;
		for (int j = 1; j <= tot && i*prime[j] < N; j++) {
			isp[i*prime[j]] = 1, pre[i*prime[j]] = prime[j];
			if (i%prime[j] == 0) break;
		}
	}
}
int main() {
	scanf("%d%d", &n, &k);
	prepa();
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a);
		tot = 0;
		while(pre[a]) {
			if (pre[a] == l[tot]) ++cnt[tot];
			else l[++tot] = pre[a], cnt[tot] = 1;
			a /= pre[a];
		}
		if (a == l[tot]) ++cnt[tot];
		else l[++tot] = a, cnt[tot] = 1;
		t.clear();
		for (int i = 1; i <= tot; i++)
			if (cnt[i]%k && l[i] != 1) t[l[i]] = k-cnt[i]%k;
		ans += T[t];
		t.clear();
		for (int i = 1; i <= tot; i++)
			if (cnt[i]%k && l[i] != 1) t[l[i]] = cnt[i]%k;
		T[t]++;
	}
	cout << ans << "\n";
	return 0;
}


#include<bits/stdc++.h>
#define MP make_pair
#define PB emplace_back
using namespace std;
typedef long long ll;
template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0' || ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&& ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
const int N=1e5+10;
int n,k,i,j,a[N],primes[N],cnt[N];
ll mx=10000000000ll,res=0;
vector<int>g[N];
ll ksc(ll x,ll y,ll mx){
    ll res=0;
    while (y){
        if (y&1) res=res+x;
        if (res>mx) return -1;
        x=x+x;
        if (x>mx) return -1;
        y>>=1;
    }
    return res;
}
ll fexp(ll a,ll n){
	ll res=1;
	while (n){
		if (a==-1) return -1;
		if (n&1){
			ll tt=ksc(res,a,mx);
			if (tt==-1) return -1;
			res=tt;
		}
		n>>=1;
		ll tt=ksc(a,a,mx);
		a=tt;
	}
	return res;
}
void init(){
	for (i=2;i<=1e5;++i){
		if (!primes[i]) primes[++primes[0]]=i;
		for (j=1;j<=primes[0] && i*primes[j]<=1e5;++j){
			primes[i*primes[j]]=1;
			if (i%primes[j]==0) break;
		}
	}
}
int main(){
	read(n),read(k);
	for (i=1;i<=n;++i) read(a[i]);
	if (k==2){
		init();
		for (i=1;i<=n;++i){
			for (j=1;j<=primes[0];++j){
				if ((ll)primes[j]*primes[j]>a[i]) break;
				int t=primes[j]*primes[j];
				while (a[i]%t==0) a[i]/=t;
				if (t>a[i]) break;
			}
			res+=cnt[a[i]];
			cnt[a[i]]+=1;
		}
		printf("%lld\n",res);
	}
	else{
		for (i=1;i<=n;++i) cnt[a[i]]+=1;
		ll num=1,j=1;
		for (;;j++){
			num=fexp(j,k);
			// cout<<num<<endl;
			if (num==-1) break;
			ll m=sqrt(num+0.5);
			if (num<=1e5){
				if (num==1) res+=(ll)(cnt[1]-1)*cnt[1]/2;
				else res+=(ll)cnt[num]*cnt[1];
			}
			// cout<<num<<endl;
			for (i=2;i<=m;++i){
				if (num%i==0){
					if (num/i==i){
						if (i<=1e5) res+=(ll)(cnt[i]-1)*cnt[i]/2;
					}
					else if (num/i<=1e5 && i<=1e5) res+=(ll)cnt[num/i]*cnt[i];
				}
			}
			// cout<<num<<" "<<res<<endl;
		}
		printf("%lld\n",res);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int N=2e5+5;
ll a[N];
ll b[N],c[N];
int p[N],tot;
bool noprime[N];
vector<int>v[N];
void init(){
	for(int i=2;i<N;i++){
	    if(!noprime[i])p[++tot]=i;
	    for(int j=1;j<=tot,i*p[j]<N;j++){
	        noprime[i*p[j]]=1;
	        if(!(i%p[j]))break;
	    }
	}
}
map<ll,ll>mp;
int main(){
	init();
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		int t=a[i];
		b[i]=c[i]=1;
		for(int j=1;j<=tot&&1ll*p[j]*p[j]<=t;j++){
			if(t%p[j]==0){
				int cnt=0;
				b[i]*=p[j];
				while(t%p[j]==0)t/=p[j],cnt++;
				for(int z=1;z<=cnt%k;z++)
					c[i]*=p[j];
				v[i].push_back(p[j]);
			}
		}
		if(t!=1)b[i]*=t,c[i]*=t;
		//printf("%lld %lld\n",b[i],c[i]);
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ll tmp=1;
		bool flag=0;
		for(int j=1;j<=k;j++){
			tmp*=b[i];

			if(tmp>1e11){
				flag=1;
				break;
			}

		}
		if(!flag){
			ll res=tmp/c[i];
			for(auto j:v[i]){
				ll tt=res;
				int cnt=0;
				while(tt%j==0)tt/=j,cnt++;
				if(cnt==k)res=tt;
			}
			if(mp.count(res))ans+=mp[res];

		}
		mp[c[i]]++;
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define MXN 200005
int vval[1000005];
int num[1000005][4];

int main(){
    int N,m,q;
    scanf("%d %d %d",&N,&m,&q);
    int cnt=1;
    for(int i = 1;i<=N;i++){
        for(int j=1;j<=m;j++){
            int x;//=rand();
            scanf("%d",&x);
            vval[cnt]=x;
            if(j!=m){
                num[cnt][1]=cnt+1;
                num[cnt+1][3]=cnt;
            }
            if(i!=N){
                num[cnt][2]=cnt+m;
                num[cnt+m][0]=cnt;
            }
            cnt++;

        }
    }/*{
        int val=1;
    while(num[val][0])val=num[val][0];
    while(num[val][3])val=num[val][3];
    for(int i = 1;i<=N;i++){
        int tmp=val;
        val=num[val][2];
        for(int j=1;j<=m;j++){
            printf("%d ",vval[tmp]);
            tmp=num[tmp][1];
        }
        printf("\n");
    }
    }*/
    while(q--){
        int a=1,b=50,c=2,d=100,h=999,w=1;
        scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&h,&w);
        int val=1;
        while(num[val][0])val=num[val][0];
        while(num[val][3])val=num[val][3];
        int ta=val;
        for(int i=1;i<a;i++)ta=num[ta][2];
        for(int i=1;i<b;i++)ta=num[ta][1];
        int tb=val;
        for(int i=1;i<c;i++)tb=num[tb][2];
        for(int i=1;i<d;i++)tb=num[tb][1];
        num[num[ta][0]][2]=tb;
        num[num[tb][0]][2]=ta;
        swap(num[ta][0],num[tb][0]);
        for(int i=1;i<w;i++){
            ta=num[ta][1];
            tb=num[tb][1];
            num[num[ta][0]][2]=tb;
            num[num[tb][0]][2]=ta;
            swap(num[ta][0],num[tb][0]);
        }
        num[num[ta][1]][3]=tb;
        num[num[tb][1]][3]=ta;
        swap(num[ta][1],num[tb][1]);
        for(int i=1;i<h;i++){
            ta=num[ta][2];
            tb=num[tb][2];
            num[num[ta][1]][3]=tb;
            num[num[tb][1]][3]=ta;
            swap(num[ta][1],num[tb][1]);
        }
        num[num[ta][2]][0]=tb;
        num[num[tb][2]][0]=ta;
        swap(num[ta][2],num[tb][2]);
        for(int i = 1;i<w;i++){
            ta=num[ta][3];
            tb=num[tb][3];
            num[num[ta][2]][0]=tb;
            num[num[tb][2]][0]=ta;
            swap(num[ta][2],num[tb][2]);
        }
        num[num[ta][3]][1]=tb;
        num[num[tb][3]][1]=ta;
        swap(num[ta][3],num[tb][3]);
        for(int i=1;i<h;i++){
            ta=num[ta][0];
            tb=num[tb][0];
            num[num[ta][3]][1]=tb;
            num[num[tb][3]][1]=ta;
            swap(num[ta][3],num[tb][3]);
        }
        val=1;
      /*  printf("?\n");
        while(num[val][0])val=num[val][0],printf("%d\n",val),getchar();
        while(num[val][3])val=num[val][3];
        for(int i = 1;i<=N;i++){
            int tmp=val;
            val=num[val][2];
            for(int j=1;j<=m;j++){
                printf("%d ",vval[tmp]);
                tmp=num[tmp][1];
            }
            printf("\n");
        }*/
    }
    int val=1;
    while(num[val][0])val=num[val][0];
    while(num[val][3])val=num[val][3];
    for(int i = 1;i<=N;i++){
        int tmp=val;
        val=num[val][2];
        for(int j=1;j<=m;j++){
            printf("%d ",vval[tmp]);
            tmp=num[tmp][1];
        }
        printf("\n");
    }
}
/**/


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define MXN 200005
struct node{
    node *l,*r;
    int val;
    int Size;
    int pri;
    node(int _val):l(NULL),r(NULL),val(_val),Size(1),pri(rand()){}
}*root;
int Size(node *n){
    return n?n->Size:0;
}
void pull(node *n){
    n->Size=Size(n->l)+Size(n->r)+1;
}
node *merge(node *l,node *r){
    if(!l||!r)return l?l:r;
    if(l->pri>r->pri){
        l->r=merge(l->r,r);
        pull(l);
        return l;
    }
    else{
        r->l=merge(l,r->l);
        pull(r);
        return r;
    }
}
void split(node *t,int k,node *&l,node *&r){
    if(!t)l=r=NULL;
    else if(Size(t->l)<k){
        l=t;
        split(t->r,k-Size(t->l)-1,l->r,r);
        pull(l);
    }
    else{
        r=t;
        split(t->l,k,l,r->l);
        pull(r);
    }
}
node *n[1005];
void print(node *n){
    if(n){
        print(n->l);
        printf("%d ",n->val);
        print(n->r);
    }
}
int main(){
    int N,m,q;
    scanf("%d %d %d",&N,&m,&q);
    for(int i = 1;i<=N;i++){
        for(int j=1;j<=m;j++){
            int x;
            scanf("%d",&x);
            n[i]=merge(n[i],new node(x));
        }
    }
    while(q--){
        int a,b,c,d,h,w;
        scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&h,&w);
        if(a==c){
            if(b>d)swap(b,d);
        }
        for(int j=h-1;j>=0;j--){
            if(a!=c){
                node *al,*amid,*ar;
                node *bl,*bmid,*br;
                split(n[c+j],d-1,bl,bmid);
                split(bmid,w,bmid,br);
                split(n[a+j],b-1,al,amid);
                split(amid,w,amid,ar);
                n[a+j]=merge(al,merge(bmid,ar));
                n[c+j]=merge(bl,merge(amid,br));
            }
            else{
                node *al,*amid,*ar,*bmid,*br;
                split(n[c+j],d-1,ar,bmid);
                split(bmid,w,bmid,br);
                split(ar,b-1,al,ar);
                split(ar,w,amid,ar);
                n[c+j]=merge(al,merge(bmid,merge(ar,merge(amid,br))));
            }
        }
    }

    for(int i = 1;i<=N;i++)
    print(n[i]),printf("\n");
}
/**/


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define MXN 200005
struct node{
    node *l,*r;
    int val;
    int Size;
    node(int _val):l(NULL),r(NULL),val(_val),Size(1){}
}*root;
int Size(node *n){
    return n?n->Size:0;
}
void pull(node *n){
    n->Size=Size(n->l)+Size(n->r)+1;
}
node *merge(node *l,node *r){
    if(!l||!r)return l?l:r;
    if(rand()%(Size(l)+Size(r))<Size(l)){
        l->r=merge(l->r,r);
        pull(l);
        return l;
    }
    else{
        r->l=merge(l,r->l);
        pull(r);
        return r;
    }
}
void split(node *t,int k,node *&l,node *&r){
    if(!t)l=r=NULL;
    else if(Size(t->l)<k){
        l=t;
        split(t->r,k-Size(t->l)-1,l->r,r);
        pull(l);
    }
    else{
        r=t;
        split(t->l,k,l,r->l);
        pull(r);
    }
}
node *n[1005];
void print(node *n){
    if(n){
        print(n->l);
        printf("%d ",n->val);
        print(n->r);
    }
}
int main(){
    int N,m,q;
    scanf("%d %d %d",&N,&m,&q);
    for(int i = 1;i<=N;i++){
        for(int j=1;j<=m;j++){
            int x;
            scanf("%d",&x);
            n[i]=merge(n[i],new node(x));
        }
    }
    while(q--){
        int a,b,c,d,h,w;
        scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&h,&w);
        for(int j=h-1;j>=0;j--){
            node *al,*amid,*ar;
            node *bl,*bmid,*br;

          //  printf("? %d %d\n",Size(n[a+j]),Size(n[b+j]));
            split(n[c+j],d-1,bl,bmid);
           // printf("%d %d %d\n",Size(bl),Size(bmid),d-1);
            split(bmid,w,bmid,br);
            split(n[a+j],b-1,al,amid);
            split(amid,w,amid,ar);

           // printf("? %d %d %d %d %d %d %d %d\n",Size(al),Size(amid),Size(ar),Size(bl),Size(bmid),Size(br),b,d);
            n[a+j]=merge(al,merge(bmid,ar));
            n[c+j]=merge(bl,merge(amid,br));
          //  printf("! %d %d\n",Size(n[a+j]),Size(n[c+j]));
        }
       // for(int i = 1;i<=N;i++)
        //  print(n[i]),printf("\n");
    }

    for(int i = 1;i<=N;i++)
    print(n[i]),printf("\n");
}
/**/


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define MXN 200005
struct node{
    node *l,*r;
    int val;
    int Size;
    int pri;
    node(int _val):l(NULL),r(NULL),val(_val),Size(1),pri(rand()){}
}*root;
int Size(node *n){
    return n?n->Size:0;
}
void pull(node *n){
    n->Size=Size(n->l)+Size(n->r)+1;
}
node *merge(node *l,node *r){
    if(!l||!r)return l?l:r;
    if(l->pri>r->pri){
        l->r=merge(l->r,r);
        pull(l);
        return l;
    }
    else{
        r->l=merge(l,r->l);
        pull(r);
        return r;
    }
}
void split(node *t,int k,node *&l,node *&r){
    if(!t)l=r=NULL;
    else if(Size(t->l)<k){
        l=t;
        split(t->r,k-Size(t->l)-1,l->r,r);
        pull(l);
    }
    else{
        r=t;
        split(t->l,k,l,r->l);
        pull(r);
    }
}
node *n[1005];
void print(node *n){
    if(n){
        print(n->l);
        printf("%d ",n->val);
        print(n->r);
    }
}
int main(){
    int N,m,q;
    scanf("%d %d %d",&N,&m,&q);
    for(int i = 1;i<=N;i++){
        for(int j=1;j<=m;j++){
            int x;
            scanf("%d",&x);
            n[i]=merge(n[i],new node(x));
        }
    }
    while(q--){
        int a,b,c,d,h,w;
        scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&h,&w);
        for(int j=h-1;j>=0;j--){
            node *al,*amid,*ar;
            node *bl,*bmid,*br;

          //  printf("? %d %d\n",Size(n[a+j]),Size(n[b+j]));
            split(n[c+j],d-1,bl,bmid);
           // printf("%d %d %d\n",Size(bl),Size(bmid),d-1);
            split(bmid,w,bmid,br);
            split(n[a+j],b-1,al,amid);
            split(amid,w,amid,ar);

           // printf("? %d %d %d %d %d %d %d %d\n",Size(al),Size(amid),Size(ar),Size(bl),Size(bmid),Size(br),b,d);
            n[a+j]=merge(al,merge(bmid,ar));
            n[c+j]=merge(bl,merge(amid,br));
          //  printf("! %d %d\n",Size(n[a+j]),Size(n[c+j]));
        }
       // for(int i = 1;i<=N;i++)
        //  print(n[i]),printf("\n");
    }

    for(int i = 1;i<=N;i++)
    print(n[i]),printf("\n");
}
/**/


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <iterator>
using namespace std;

bool cmp(int a, int b)
{
	return a < b;
}

int main()
{
	int n, m;
	int p[1005][1005];
	int k;
	while (cin >> n >> m >> k)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> p[i][j];
			}
		}
		while (k--)
		{
			int a, b, c, d, e, f;
			cin >> a >> b >> c >> d >> e >> f;
			for (int i = 0; i < e; i++)
			{
				for (int j = 0; j < f; j++)
				{
					swap(p[a + i][b + j], p[c + i][d + j]);
				}
			}
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cout << p[i][j] << (j == m ? "\n" : " ");
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define N 1010
struct Node{
	int right,down;
	int v;
}_Node[N*N];
int a[N][N],b[N][N];
int idx;

int move(int p,int x,int y){
	while(x--){
		p = _Node[p].down;
	}
	while(y--){
		p = _Node[p].right;
	}
	return p;
}

void print(int n,int m){
	int p = b[0][0];
	for(int i=1;i<=n;i++){
		p = move(p,1,0);
		int ip = p;
		for(int j=0;j<m;j++){
			ip = _Node[ip].right;
			cout<<_Node[ip].v;
			if(j+1==m) cout<<endl;
			else cout<<" ";
		}
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,m,q;
	while(cin>>n>>m>>q){
		idx = 0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>a[i][j];
			}
		}
		for(int i=0;i<=n;i++){
			for(int j=0;j<=m;j++){
				b[i][j] = idx++;
				_Node[b[i][j]].v = a[i][j];
				if(j){
					_Node[b[i][j-1]].right = b[i][j];
				}
				if(i){
					_Node[b[i-1][j]].down = b[i][j];
				}
			}
		}
		//print(n,m);
		while(q--){
			int ai,bi,ci,di,hi,wi;
			cin>>ai>>bi>>ci>>di>>hi>>wi;

			int p1 = move(b[0][0],ai-1,bi-1);
			int p2 = move(b[0][0],ci-1,di-1);

			int dp1 = move(p1,1,0);
			int dp2 = move(p2,1,0);

			for(int i=0;i<hi;i++){
				swap(_Node[dp1].right,_Node[dp2].right);
				dp1 = move(dp1,1,0);
				dp2 = move(dp2,1,0);
			}

			int rp1 = move(p1,0,1);
			int rp2 = move(p2,0,1);
			for(int i=0;i<wi;i++){
				swap(_Node[rp1].down,_Node[rp2].down);
				rp1 = move(rp1,0,1);
				rp2 = move(rp2,0,1);
			}

			dp1 = move(p1,1,wi);
			dp2 = move(p2,1,wi);

			for(int i=0;i<hi;i++){
				swap(_Node[dp1].right,_Node[dp2].right);
				dp1 = move(dp1,1,0);
				dp2 = move(dp2,1,0);
			}

			rp1 = move(p1,hi,1);
			rp2 = move(p2,hi,1);

			for(int i=0;i<wi;i++){
				swap(_Node[rp1].down,_Node[rp2].down);
				rp1 = move(rp1,0,1);
				rp2 = move(rp2,0,1);
			}
			//cout<<q<<":"<<endl;
			//print(n,m);
		}
		print(n,m);
	}
	return 0;

}


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1010;

struct node
{
	int right,down;
	int v;
}da[maxn*maxn];

int move(int p, int x, int y)
{
	while (x--)
	{
		p = da[p].down;
	}
	while (y--)
	{
		p = da[p].right;
	}
	return p;
}

int a[maxn][maxn], b[maxn][maxn];
void print(int n, int m) {
	int p = b[0][0];
	for (int i = 1; i <= n; i++) {
		p = move(p, 1, 0);
		int ip = p;
		for (int j = 0; j < m; j++) {
			ip = da[ip].right;
			cout << da[ip].v;
			if (j + 1 == m) cout << endl;
			else cout << " ";
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	int n, m, k;
	while (cin >> n >> m >> k)
	{
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		int t = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				scanf("%d",&a[i][j]);
			}
		}

		for (int i=0; i <= n; i++)
		{
			for (int j = 0; j <= n; j++)
			{
				b[i][j] = t++;
				da[b[i][j]].v = a[i][j];
				if (i)
					da[b[i - 1][j]].down = b[i][j];
				if (j)
					da[b[i][j - 1]].right = b[i][j];
			}
		}

		while (k--)
		{
			int x1, y1, x2, y2, w, h;
			cin >> x1 >> y1 >> x2 >> y2 >> w >> h;

			int p1 = move(b[0][0], x1-1, y1-1);
			int p2 = move(b[0][0], x2-1, y2-1);

			int rp1, rp2;//right point
			int dp1, dp2;//down point

			rp1 = move(p1, 1, 0);
			rp2 = move(p2, 1, 0);
			for (int i = 0; i < w; i++)
			{
				swap(da[rp1].right, da[rp2].right);
				rp1 = da[rp1].down;
				rp2 = da[rp2].down;
			}

			dp1 = move(p1, 0, 1);
			dp2 = move(p2, 0, 1);
			for (int i = 0; i < h; i++)
			{
				swap(da[dp1].down,da[dp2].down);
				dp1 = da[dp1].right;
				dp2 = da[dp2].right;
			}

			rp1 = move(p1, 1, h);
			rp2 = move(p2, 1, h);
			for (int i = 0; i < w; i++)
			{
				swap(da[rp1].right, da[rp2].right);
				rp1 = da[rp1].down;
				rp2 = da[rp2].down;
			}

			dp1 = move(p1, w, 1);
			dp2 = move(p2, w, 1);
			for (int i = 0; i < h; i++)
			{
				swap(da[dp1].down, da[dp2].down);
				dp1 = da[dp1].right;
				dp2 = da[dp2].right;
			}
		}
		print(n, m);
	}
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int maxn = 1010;

struct node
{
	int right,down;
	int v;
}da[maxn*maxn];

int move(int p, int x, int y)
{
	while (x--)
	{
		p = da[p].down;
	}
	while (y--)
	{
		p = da[p].right;
	}
	return p;
}

int a[maxn][maxn], b[maxn][maxn];
void print(int n, int m) {
	int p = b[0][0];
	for (int i = 1; i <= n; i++) {
		p = move(p, 1, 0);
		int ip = p;
		for (int j = 0; j < m; j++) {
			ip = da[ip].right;
			cout << da[ip].v;
			if (j + 1 == m) cout << endl;
			else cout << " ";
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	int n, m, k;
	while (cin >> n >> m >> k)
	{
		int t = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				scanf("%d",&a[i][j]);
			}
		}

		for (int i=0; i <= n; i++)
		{
			for (int j = 0; j <= n; j++)
			{
				b[i][j] = t++;
				da[b[i][j]].v = a[i][j];
				if (i)
					da[b[i - 1][j]].down = b[i][j];
				if (j)
					da[b[i][j - 1]].right = b[i][j];
			}
		}

		while (k--)
		{
			int x1, y1, x2, y2, w, h;
			cin >> x1 >> y1 >> x2 >> y2 >> w >> h;

			int p1 = move(b[0][0], x1-1, y1-1);
			int p2 = move(b[0][0], x2-1, y2-1);

			int rp1, rp2;//right point
			int dp1, dp2;//down point

			rp1 = move(p1, 1, 0);
			rp2 = move(p2, 1, 0);
			for (int i = 0; i < w; i++)
			{
				swap(da[rp1].right, da[rp2].right);
				rp1 = da[rp1].down;
				rp2 = da[rp2].down;
			}

			dp1 = move(p1, 0, 1);
			dp2 = move(p2, 0, 1);
			for (int i = 0; i < h; i++)
			{
				swap(da[dp1].down,da[dp2].down);
				dp1 = da[dp1].right;
				dp2 = da[dp2].right;
			}

			rp1 = move(p1, 1, h);
			rp2 = move(p2, 1, h);
			for (int i = 0; i < w; i++)
			{
				swap(da[rp1].right, da[rp2].right);
				rp1 = da[rp1].down;
				rp2 = da[rp2].down;
			}

			dp1 = move(p1, w, 1);
			dp2 = move(p2, w, 1);
			for (int i = 0; i < h; i++)
			{
				swap(da[dp1].down, da[dp2].down);
				dp1 = da[dp1].right;
				dp2 = da[dp2].right;
			}
		}
		print(n, m);
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1010;

struct node
{
	int right,down;
	int v;
}da[maxn*maxn];
int a[maxn][maxn], b[maxn][maxn];
int i,j;
int t;

int move(int p, int x, int y)
{
	while (x--)
	{
		p = da[p].down;
	}
	while (y--)
	{
		p = da[p].right;
	}
	return p;
}

void print(int n, int m) {
	int p = b[0][0];
	for (i = 1; i <= n; i++) {
		p = move(p, 1, 0);
		int ip = p;
		for (j = 0; j < m; j++) {
			ip = da[ip].right;
			printf("%d",da[ip].v);
			if (j + 1 == m) printf("\n");
			else printf(" ");
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	int n, m, k;
	while (cin >> n >> m >> k)
	{
		t = 0;
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= m; j++)
			{
				scanf("%d",&a[i][j]);
			}
		}

		for (i=0; i <= n; i++)
		{
			for (j = 0; j <= n; j++)
			{
				b[i][j] = t++;
				da[b[i][j]].v = a[i][j];
				if (j)
					da[b[i][j - 1]].right = b[i][j];
				if (i)
					da[b[i - 1][j]].down = b[i][j];
			}
		}

		while (k--)
		{
			int x1, y1, x2, y2, w, h;
			scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&h,&w);

			int p1 = move(b[0][0], x1-1, y1-1);
			int p2 = move(b[0][0], x2-1, y2-1);

			int rp1, rp2;//right point
			int dp1, dp2;//down point

			dp1 = move(p1, 1, 0);
			dp2 = move(p2, 1, 0);

			for (i = 0; i < h; i++)
			{
				swap(da[dp1].right, da[dp2].right);
				dp1=move(dp1,1,0);
				dp2=move(dp2,1,0);
			}

			rp1 = move(p1, 0, 1);
			rp2 = move(p2, 0, 1);
			for (i = 0; i < w; i++)
			{
				swap(da[rp1].down,da[rp2].down);
				rp1 = move(rp1,0,1);
				rp2 = move(rp2,0,1);
			}

			dp1 = move(p1, 1, w);
			dp2 = move(p2, 1, w);
			for (i = 0; i < h; i++)
			{
				swap(da[dp1].right, da[dp2].right);
				dp1=move(dp1,1,0);
				dp2=move(dp2,1,0);
			}

			rp1 = move(p1, h, 1);
			rp2 = move(p2, h, 1);
			for (i = 0; i < w; i++)
			{
				swap(da[rp1].down, da[rp2].down);
				rp1 = move(rp1,0,1);
				rp2 = move(rp2,0,1);
			}
		}
		print(n, m);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define __ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define LOG1(x1,x2) cout << x1 << ": " << x2 << endl;
#define LOG2(x1,x2,y1,y2) cout << x1 << ": " << x2 << " , " << y1 << ": " << y2 << endl;
#define LOG3(x1,x2,y1,y2,z1,z2) cout << x1 << ": " << x2 << " , " << y1 << ": " << y2 << " , " << z1 << ": " << z2 << endl;
typedef long long ll;
typedef double db;
const int N = 2*1e6+100;
const int M = 1e5+100;
const db EPS = 1e-9;
using namespace std;

int n,m,q;
struct Node{
	int r,d,v;
}t[N];

int Pos(int x,int y){
	x++, y++;
	return (x-1)*(m+1)+y;
}

int main()
{
	scanf("%d%d%d",&n,&m,&q);
	rep(i,1,n)
		rep(j,1,m) scanf("%d",&t[Pos(i,j)].v);
	rep(i,0,n)
		rep(j,0,m){
			t[Pos(i,j)].r = Pos(i,j+1);
			t[Pos(i,j)].d = Pos(i+1,j);
		}
	rep(i,1,q){
		int a,b,c,d,h,w;
		scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&h,&w);
		int x = 1,y = 1,u,v;
		rep(j,1,a-1) x = t[x].d;
		rep(j,1,b-1) x = t[x].r;
		rep(j,1,c-1) y = t[y].d;
		rep(j,1,d-1) y = t[y].r;
		u = x, v = y;
		rep(j,1,h){
			u = t[u].d, v = t[v].d;
			swap(t[u].r,t[v].r);
		}
		rep(j,1,w){
			u = t[u].r, v = t[v].r;
			swap(t[u].d,t[v].d);
		}
		u = x, v = y;
		rep(j,1,w){
			u = t[u].r, v = t[v].r;
			swap(t[u].d,t[v].d);
		}
		rep(j,1,h){
			u = t[u].d, v = t[v].d;
			swap(t[u].r,t[v].r);
		}
	}
	int x = 1;
	x = t[x].d, x = t[x].r;
	rep(i,1,n){
		int y = x;
		rep(j,1,m){
			printf("%d",t[y].v);
			y = t[y].r;
			if(j == m) printf("\n");
			else printf(" ");
		}
		x = t[x].d;
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 2000000;
int n, m, k;
int stid;
int tot, stx, sty;
int f[N], Next[N][2], Pre[N][2];
int id(int x, int y) {
    if (x > n || y > m || !x || !y) return 0;
    return (x - 1) * m + y;
}
int loc(int x, int y) {
    int outid = stid;
    while (--x) outid = Next[outid][0];
    while (--y) outid = Next[outid][1];
    return outid;
}
void dfs(int id, int x, int y) {
    if (y == m)
        printf("%d\n", f[id]);
    else {
        printf("%d ", f[id]);
        dfs(Next[id][1], x, y + 1);
        if (y == 1 && x < n)
            dfs(Next[id][0], x + 1, y);
    }
}
void link(int x, int y, bool sig) {
    if (!x) Pre[y][sig] = 0;
    else{
        if (!y) Next[x][sig] = 0;
        else
            Pre[y][sig] = x, Next[x][sig] = y;
    }
}
int main() {
    stid = 1;
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 1; i <=n; i ++)
        for (int j = 1; j <= m; j ++) {
            scanf("%d", &f[id(i, j)]);
            Next[id(i, j)][0] = id(i+ 1,j);
            Next[id(i, j)][1] = id(i,j+1);
            Pre[id(i, j)][0] = id(i- 1,j);
            Pre[id(i, j)][1] = id(i,j-1);
        }
    while (k --) {
        int x, y, xx, yy, h, w;
        scanf("%d %d %d %d %d %d", &x, &y, &xx, &yy, &h, &w);
        int id1 = loc(x, y);
        int id2 = loc(xx, yy);
        x = id1;
        xx = id2;
        for (int i = 0; i < h; i ++)
        {
            int l = Pre[x][1], ll = Pre[xx][1];
            link(l, xx, 1), link(ll, x, 1);
            if (i != h - 1) {
                x = Next[x][0];
                xx = Next[xx][0];
            }
        }
        for (int i = 0; i < w; i ++) {
            int l = Next[x][0], ll = Next[xx][0];
            link(xx, l, 0), link(x, ll, 0);
            if (i != w - 1) {
                x = Next[x][1];
                xx = Next[xx][1];
            }
        }
        for (int i = 0; i < h; i ++)
        {
            int l = Next[x][1], ll = Next[xx][1];
            link(xx, l, 1), link(x, ll, 1);
            if (i != h - 1) {
                x = Pre[x][0];
                xx = Pre[xx][0];
            }
        }
        for (int i = 0; i < w; i ++) {
            int l = Pre[x][0], ll = Pre[xx][0];
            link(l, xx, 0), link(ll, x, 0);
            if (i != w - 1) {
                x = Pre[x][1];
                xx = Pre[xx][1];
            }
        }
        if (!Pre[x][0] && !Pre[x][1]) {
            stid = x;
        }

        if (!Pre[xx][0] && !Pre[xx][1]) {
            stid = xx;
        }
    }
    dfs(stid, 1, 1);
    return 0;
}


#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
	out << "(" << a.first << "," << a.second << ")";
	return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
	out << "["; bool first = true;
	for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
	return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
	out << "["; bool first = true;
	for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
	return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
	out << "{"; bool first = true;
	for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
	return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
	out << "{"; bool first = true;
	for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
	return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
	cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << ": " << arg1 << " |";
	__f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
	fast_ios() {
		cin.tie(nullptr);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(10);
	};
} fast_ios_;

struct Node {
	Node *left, *right, *up, *down;
	int val;
};

const int N = 1e3 + 10;
Node a[N][N];

int main() {
	int n, m, q;
	cin >> n >> m >> q;

	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			if (i - 1 >= 0) a[i][j].up = &a[i - 1][j];
			if (i + 1 < N) a[i][j].down = &a[i + 1][j];
			if (j - 1 >= 0) a[i][j].left = &a[i][j - 1];
			if (j + 1 < N) a[i][j].right = &a[i][j + 1];
		}
	}

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> a[i][j].val;
		}
	}

	while (q--) {
		int x1, y1, x2, y2, h, w;
		cin >> x1 >> y1 >> x2 >> y2 >> h >> w;

		Node* r1 = a[x1];
		while (y1--) r1 = r1->right;
		Node* r2 = a[x2];
		while (y2--) r2 = r2->right;
		// trace(r1->val, r2->val);

		Node *p1 = r1, *p2 = r2;
		for (int j = 0; j < w; ++j) {
			p1->up->down = p2;
			p2->up->down = p1;
			swap(p1->up, p2->up);
			p1 = p1->right; p2 = p2->right;
		}
		p1 = p1->left; p2 = p2->left;
		// trace(p1->val, p2->val);

		for (int i = 0; i < h; ++i) {
			r1->left->right = r2;
			r2->left->right = r1;
			swap(r1->left, r2->left);

			p1->right->left = p2;
			p2->right->left = p1;
			swap(p1->right, p2->right);

			r1 = r1->down; r2 = r2->down;
			p1 = p1->down; p2 = p2->down;
		}
		p1 = p1->up; p2 = p2->up;

		for (int j = 0; j < w; ++j) {
			p1->down->up = p2;
			p2->down->up = p1;
			swap(p1->down, p2->down);
			p1 = p1->left; p2 = p2->left;
		}
	}

	for (int i = 1; i <= n; ++i) {
		Node* r = a[i][0].right;
		for (int j = 0; j < m; ++j) {
			cout << r->val << " \n"[j + 1 == m];
			r = r->right;
		}
	}

	return 0;
}


#include "bits/stdc++.h"
using namespace std;
const int N = 1e3 + 9;
const int S = N * N;
int n , m , q;
int arr[S];
int rgt[S];
int dwn[S];
inline int convert(int r , int c){
    return r * (m + 2) + c;
}
int main(){
    scanf("%d %d %d" , &n , &m , &q);
    for(int i = 1 ; i <= n ; ++i){
        for(int j = 1 ; j <= m ; ++j){
            scanf("%d" , &arr[convert(i , j)]);
        }
    }
    for(int i = 0 ; i <= n ; ++i){
        for(int j = 0 ; j <= m ; ++j){
            dwn[convert(i , j)] = convert(i + 1 , j);
            rgt[convert(i , j)] = convert(i , j + 1);
        }
    }
    while(q--){
        int x1 , y1 , x2 , y2 , r , c;
        scanf("%d %d %d %d %d %d" , &x1 , &y1 , &x2 , &y2 , &r , &c);
        int cell1 = convert(x1 , 0);
        int cell2 = convert(x2 , 0);
        int cell3 = convert(0 , y1);
        int cell4 = convert(0 , y2);
        for(int i = 1 ; i < y1 ; ++i){
            cell1 = rgt[cell1];
        }
        for(int i = 1 ; i < y2 ; ++i){
            cell2 = rgt[cell2];
        }
        for(int i = 1 ; i < x1 ; ++i){
            cell3 = dwn[cell3];
        }
        for(int i = 1 ; i < x2 ; ++i){
            cell4 = dwn[cell4];
        }
        for(int i = 0 ; i < r ; ++i){
            swap(rgt[cell1] , rgt[cell2]);
            if(i + 1 < r){
                cell1 = dwn[cell1];
                cell2 = dwn[cell2];
            }
            else{
                cell1 = rgt[cell1];
                cell2 = rgt[cell2];
            }
        }
        for(int i = 0 ; i < c ; ++i){
            swap(dwn[cell1] , dwn[cell2]);
            cell1 = rgt[cell1];
            cell2 = rgt[cell2];
        }
        for(int i = 0 ; i < c ; ++i){
            swap(dwn[cell3] , dwn[cell4]);
            if(i + 1 < c){
                cell3 = rgt[cell3];
                cell4 = rgt[cell4];
            }
            else{
                cell3 = dwn[cell3];
                cell4 = dwn[cell4];
            }
        }
        for(int i = 0 ; i < r ; ++i){
            swap(rgt[cell3] , rgt[cell4]);
            cell3 = dwn[cell3];
            cell4 = dwn[cell4];
        }
    }
    for(int i = 1 ; i <= n ; ++i){
        int cell = convert(i , 0);
        for(int j = 1 ; j <= m ; ++j){
            cell = rgt[cell];
            printf("%d%c" , arr[cell] , " \n"[j == m]);
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> pr;
typedef pair<LL, LL> lpr;
typedef pair<double, double> dpr;
typedef pair<long double, long double> ldpr;
//typedef complex<double> cp;

#define dbgs(x) #x << " = " << x
#define dbgs2(x, y) dbgs(x) << ", " << dbgs(y)
#define dbgs3(x, y, z) dbgs2(x, y) << ", " << dbgs(z)
#define dbgs4(w, x, y, z) dbgs3(w, x, y) << ", " << dbgs(z)

#define mst(s, x) memset(s, x, sizeof(s))
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define all(x) x.begin(), x.end()
#define unq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define rg register
#define fp(i, a, b) for (rg int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (rg int i = (a), I = (b)-1; i > I; --i)
#define ub upper_bound
#define lb lower_bound
#define bitcount __builtin_popcount
//#define sqr(x) ((x)*(x))

#define mid ((l+r)>>1)

#define go(u) for (rg int i = pre[u], v = e[i].v; i; i = e[i].next, v = e[i].v)
#define gow(u) for (rg LL i = pre[u], v = e[i].v, w = e[i].w; i; i = e[i].next, v = e[i].v, w = e[i].w)
#define EDGE(N,M) struct edge{int u,v,w,next;}e[M];int pre[N],edge_cnt=0;void addedge(int u,int v,int w=0){edge_cnt=edge_cnt+1,e[edge_cnt]={u,v,w,pre[u]},pre[u]=edge_cnt;}
#define CLEAR_EDGE(N) {edge_cnt=0,memset(pre,0,sizeof(int)*(N+1));}

#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
#define FASTIO struct IO { char buf[MAXSIZE], *p1, *p2;char pbuf[MAXSIZE], *pp;     IO() : p1(buf), p2(buf), pp(pbuf){}     ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }     inline char gc()     {         if (p1 == p2)             p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);         return p1 == p2 ? -1 : *p1++;     }     inline bool blank(char ch)     {         return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';     }     template <class T>     inline void read(T &x)     {         register double tmp = 1;         register bool sign = 0;         x = 0;         register char ch = gc();         for (; !isdigit(ch); ch = gc())             if (ch == '-')                 sign = 1;         for (; isdigit(ch); ch = gc())             x = x * 10 + (ch - '0');         if (ch == '.')             for (ch = gc(); isdigit(ch); ch = gc())                 tmp /= 10.0, x += tmp * (ch - '0');         if (sign)             x = -x;     }     inline void read(char *s)     {         register char ch = gc();         for (; blank(ch); ch = gc())             ;         for (; !blank(ch); ch = gc())             *s++ = ch;         *s = 0;     }     inline void read(char &c)     {         for (c = gc(); blank(c); c = gc())             ;     }     inline void push(const char &c)     {         if (pp - pbuf == MAXSIZE)             fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;         *pp++ = c;     }     template <class T>     inline void write(T x)     {         if (x < 0)             x = -x, push('-');         static T sta[35];         T top = 0;         do         {             sta[top++] = x % 10, x /= 10;         } while (x);         while (top)             push(sta[--top] + '0');     }     template <class T>     inline void write(T x, char lastChar)     {         write(x), push(lastChar);     } } io

#define MULTICASE int CASE;scanf("%d",&CASE);while(CASE--)

const int next_x[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int next_y[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int inf = 1e9+5;//7;
const LL linf = 1e18 + 5;
const double PI = acos(-1.0);
const long double eps = 1e-6;

const int MAXN = 1005;
const int N = MAXN*80+10;

const int fix = 1e6;
//const int K = 30;

#define ls (cnt<<1)
#define rs (cnt<<1|1)

/*----------head----------*/

int n,m,q;
struct node{int val; node *l,*r,*u,*d;};
node a[MAXN][MAXN];
node* gol(node *x,int cnt){while(cnt--)x=x->l;return x;}
node* gor(node *x,int cnt){while(cnt--)x=x->r;return x;}
node* gou(node *x,int cnt){while(cnt--)x=x->u;return x;}
node* god(node *x,int cnt){while(cnt--)x=x->d;return x;}
void doswapl(node *a,node *b)
{
    node *aa=a->l,*bb=b->l;
    a->l=bb,b->l=aa,aa->r=b,bb->r=a;
}
void doswapr(node *a,node *b)
{
    node *aa=a->r,*bb=b->r;
    a->r=bb,b->r=aa,aa->l=b,bb->l=a;
}
void doswapu(node *a,node *b)
{
    node *aa=a->u,*bb=b->u;
    a->u=bb,b->u=aa,aa->d=b,bb->d=a;
}
void doswapd(node *a,node *b)
{
    node *aa=a->d,*bb=b->d;
    a->d=bb,b->d=aa,aa->u=b,bb->u=a;
}

int work()
{
    scanf("%d%d%d",&n,&m,&q);
    fp(i,1,n)fp(j,1,m)
    {
        scanf("%d",&a[i][j].val);
        a[i][j].u=&a[i-1][j];
        a[i][j].d=&a[i+1][j];
        a[i][j].l=&a[i][j-1];
        a[i][j].r=&a[i][j+1];
    }
    fp(i,1,n)a[i][0].r=&a[i][1],a[i][m+1].l=&a[i][m];
    fp(j,1,m)a[0][j].d=&a[1][j],a[n+1][j].u=&a[n][j];
    while(q--)
    {
        int x1,y1,x2,y2,dd,rr;
        scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&dd,&rr);

        node *lu,*ru,*ld,*rd;
        lu=gor(&a[x1][0],y1),ru=gor(lu,rr-1);
        ld=god(lu,dd-1),rd=god(ru,dd-1);
        //cout << dbgs4(lu->val,ru->val,ld->val,rd->val) << endl;

        node *_lu,*_ru,*_ld,*_rd;
        _lu=gor(&a[x2][0],y2),_ru=gor(_lu,rr-1);
        _ld=god(_lu,dd-1),_rd=god(_ru,dd-1);
        //cout << dbgs4(_lu->val,_ru->val,_ld->val,_rd->val) << endl;

        for(node *a=lu,*b=_lu;a!=ru;a=a->r,b=b->r)doswapu(a,b); doswapu(ru,_ru);
        for(node *a=lu,*b=_lu;a!=ld;a=a->d,b=b->d)doswapl(a,b); doswapl(ld,_ld);
        for(node *a=rd,*b=_rd;a!=ld;a=a->l,b=b->l)doswapd(a,b); doswapd(ld,_ld);
        for(node *a=rd,*b=_rd;a!=ru;a=a->u,b=b->u)doswapr(a,b); doswapr(ru,_ru);
    }
    fp(i,1,n)
    {
        node *t=&a[i][0];
        fp(j,1,m)t=t->r,printf("%d ",t->val);
        printf("\n");
    }
    return 0;
}

// 分块注意维护块上标记 来更新块内数组a[]
// vector+lower_bound常数 < map/set
// map.find不会创建新元素 map[]会 注意空间
// 别对指针用memset
// 用位运算表示2^n注意加LL  1LL<<20
// 注意递归爆栈
// 注意边界
// 注意memset 多组样例会T

//#define ONLINE_JUDGE

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
#ifdef ONLINE_JUDGE
    //freopen("invazia.in", "r",stdin );
    //freopen("invazia.out","w",stdout);
#endif // ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    work();
#ifndef ONLINE_JUDGE
    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    fflush(stdout);
    cout << "time:" << clock() << "ms" << endl;
    system("pause");
#endif
    return 0;
}


#include<bits/stdc++.h>
#define REP(i,j,k) for(int i=j;i<k;++i)
#define RREP(i,j,k) for(int i=(int)j-1;i>=k;--i)
#define int long long
#define pb emplace_back
#define P pair<int,int>
#define F first
#define S second
#define ALL(x) x.begin(),x.end()
#define sz(a) (int)(a).size()
#define MX 1005
#define oo 1e18
#define mid (l+r>>1)
#define Ls l,mid,d*2
#define Rs mid+1,r,d*2+1
#define MIN(x,y) (x=min(x,(y)))
#define MAX(x,y) (x=max(x,(y)))
#define mo (int)(1e9+7)
#define IOS cin.tie(0) ,cout.tie(0), cout.sync_with_stdio(0)
using namespace std;
int n,m,q,a[MX][MX],x[2],y[2],h,w;
P d[MX][MX],r[MX][MX],p[2],p1,p2;
void R(P &v){ v = r[v.F][v.S];}
void D(P &v){ v = d[v.F][v.S];}
void SR(P a,P b){ swap(r[a.F][a.S],r[b.F][b.S]);}
void SD(P a,P b){ swap(d[a.F][a.S],d[b.F][b.S]);}
main(){
	IOS;
	cin>>n>>m>>q;
	REP(i,1,n+1) REP(j,1,m+1) cin>>a[i][j];
	REP(i,0,n+1) REP(j,0,m+1) r[i][j] = P(i,j+1), d[i][j] = P(i+1,j);
	while(q--){
		cin>>x[0]>>y[0]>>x[1]>>y[1]>>h>>w;
		p[0] = P(x[0]-1,0), p[1] = P(x[1]-1,0);
		REP(k,0,2) REP(i,0,y[k]-1) R(p[k]);
		p1 = p[0], p2 = p[1];
		REP(i,0,h) D(p1), D(p2), SR(p1,p2);
		REP(i,0,w) R(p1), R(p2), SD(p1,p2);
		p1 = p[0], p2 = p[1];
		REP(i,0,w) R(p1), R(p2), SD(p1,p2);
		REP(i,0,h) D(p1), D(p2), SR(p1,p2);
	}
	REP(i,1,n+1){
		P v = P(i,0);
		REP(j,1,m+1) R(v), cout<<a[v.F][v.S]<<" ";
		cout<<"\n";
	}
}


#include <bits/stdc++.h>
using namespace std;

const int DIM = 1005;

int mat[DIM][DIM];
pair<int, int> down[DIM][DIM], _right[DIM][DIM];

int main(void) {
#ifdef HOME
	freopen("e.in", "r", stdin);
	freopen("e.out", "w", stdout);
#endif
	int n, m, q;
	scanf("%d %d %d", &n, &m, &q);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			scanf("%d", &mat[i][j]);
	for (int i = 0; i <= n; ++i)
		for (int j = 0; j <= m; ++j) {
			down[i][j] = {i + 1, j};
			_right[i][j] = {i, j + 1};
		}
	while (q--) {
		pair<int, int> c1, c2; int h, w;
		scanf("%d %d %d %d %d %d", &c1.first, &c1.second, &c2.first, &c2.second, &h, &w);
		pair<int, int> p1(0, 0), p2(0, 0), q1, q2;
		for (int i = 1; i < c1.first; ++i)
			p1 = down[p1.first][p1.second];
		for (int j = 1; j < c1.second; ++j)
			p1 = _right[p1.first][p1.second];
		for (int i = 1; i < c2.first; ++i)
			p2 = down[p2.first][p2.second];
		for (int j = 1; j < c2.second; ++j)
			p2 = _right[p2.first][p2.second];
		q1 = p1, q2 = p2;
		for (int i = 1; i <= h; ++i) {
			q1 = down[q1.first][q1.second];
			q2 = down[q2.first][q2.second];
			swap(_right[q1.first][q1.second], _right[q2.first][q2.second]);
		}
		for (int j = 1; j <= w; ++j) {
			q1 = _right[q1.first][q1.second];
			q2 = _right[q2.first][q2.second];
			swap(down[q1.first][q1.second], down[q2.first][q2.second]);
		}
		q1 = p1, q2 = p2;
		for (int j = 1; j <= w; ++j) {
			q1 = _right[q1.first][q1.second];
			q2 = _right[q2.first][q2.second];
			swap(down[q1.first][q1.second], down[q2.first][q2.second]);
		}
		for (int i = 1; i <= h; ++i) {
			q1 = down[q1.first][q1.second];
			q2 = down[q2.first][q2.second];
			swap(_right[q1.first][q1.second], _right[q2.first][q2.second]);
		}
	}
	pair<int, int> pl(0, 0), pc;
	for (int i = 1; i <= n; ++i) {
		pl = down[pl.first][pl.second];
	 	pc = pl;
		for (int j = 1; j <= m; ++j) {
			pc = _right[pc.first][pc.second];
			printf("%d ", mat[pc.first][pc.second]);
		}
		printf("\n");
	}
	return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define MAX_SIZE 1011
using namespace std;

struct node {
    int value;
    node *next, *down;
    node(int val) {
        value = val;
    }
};

node *l[MAX_SIZE][MAX_SIZE];
int n, m;
node *getpt(int x, int y) {
    node *ret = l[0][0];
    int i;
    for (i = 0 ; i < x; i++) {
        ret = ret->down;
    }

    for (i = 0 ; i < y; i++) {
        ret = ret->next;
    }
    return ret;
}

int main() {
    int i, j, n, m, q;
    scanf("%d%d%d", &n, &m, &q);
    for (i = 0 ; i <= n + 1; i++) {
        for (j = 0 ; j <= m + 1; j++) {
            if (i >= 1 && j >= 1 && i <= n && j <= m) {
                int v;
                scanf("%d", &v);
                l[i][j] = new node(v);
            } else {
			    l[i][j] = new node(-1);
			}
        }
    }

    for (i = 0 ; i <= n + 1; i++) {
        for (j = 0 ; j <= m + 1; j++) {
            l[i][j]->down = l[i+1][j];
            l[i][j]->next = l[i][j+1];
        }
    }

    while (q > 0) {
        int a, b, c, d, w, h;
        scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &h, &w);
        node *p1 = getpt(a-1, b-1), *p2 = getpt(c-1, d-1);
        node *p3 = p1->next, *p4 = p1->down, *p5 = p2->next, *p6 = p2->down;
		for (i = 0 ; i < w; i++) {
            swap(p3->down, p5->down);
			if (i != w-1) {
                p3 = p3->next, p5 = p5->next;
            }
        }
        p3 = p3->down, p5 = p5->down;

        for (i = 0 ; i < h; i++) {
            swap(p3->next, p5->next);
			if (i != h-1) {
                p3 = p3->down, p5 = p5->down;
            }
        }

        for (i = 0 ; i < h; i++) {
            swap(p4->next, p6->next);
            if (i != h-1) {
                p4 = p4->down, p6 = p6->down;
            }
        }
        p4 = p4->next, p6 = p6->next;
        for (i = 0 ; i < w; i++) {
            swap(p4->down, p6->down);
			if (i != w-1) {
                p4 = p4->next, p6 = p6->next;
            }
        }
        q--;
    }

    for (i = 1 ; i <= n; i++) {
        node *curr = l[i][0];
        for (j = 0; j <= m; j++) {
            if (j != 0) {
                printf("%d ", curr->value);
            }
            curr = curr->next;
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::swap;
using std::vector;

class Node {
public:
    friend class Matrix;

    explicit Node(int val = 0, Node* right = nullptr, Node* down = nullptr)
        : val_(val), right_(right), down_(down) {}
    ~Node() {}

protected:
    int   val_;
    Node *right_, *down_;
};

class Matrix {
public:
    Matrix(int row_size, int col_size, int val = 0)
        : data_(row_size + 2, vector<Node*>(col_size + 2, nullptr)),
          row_size_(row_size),
          col_size_(col_size) {
        for (auto&& row : data_)
            for (auto&& i : row) i = new Node;
    }
    ~Matrix() {
        for (auto&& row : data_)
            for (auto&& i : row) delete i;
    }

    void Init() {
        for (int j = 0; j <= col_size_; ++j) {
            data_[0][j]->right_ = data_[0][j + 1];
            data_[0][j]->down_  = data_[1][j];
        }
        for (int i = 1; i <= row_size_; ++i) {
            data_[i][0]->right_ = data_[i][1];
            data_[i][0]->down_  = data_[i + 1][0];
        }
        for (int i = 1; i <= row_size_; ++i) {
            for (int j = 1; j <= col_size_; ++j) {
                int val;
                cin >> val;
                data_[i][j]->val_   = val;
                data_[i][j]->right_ = data_[i][j + 1];
                data_[i][j]->down_  = data_[i + 1][j];
            }
        }
    }

    void Print() {
        auto row_begin = data_[0][0];
        for (int i = 1; i <= row_size_; ++i) {
            if (!row_begin->down_) break;  // this statement shouldn't be run
            row_begin = row_begin->down_;
            auto cur  = row_begin;
            for (int j = 1; j <= col_size_; ++j) {
                if (!cur->right_) break;  // this statement shouldn't be run
                cur = cur->right_;
                cout << cur->val_ << (j == col_size_ ? '\n' : ' ');
            }
        }
    }

    void Swap(int row1, int col1, int row2, int col2, int height, int width) {
        auto cur1 = data_[0][0];
        auto cur2 = data_[0][0];

        // Move cur1 to data_[row1][col1]
        for (int i = 0; i < row1; ++i) cur1 = cur1->down_;
        for (int j = 0; j < col1; ++j) cur1 = cur1->right_;
        // Move cur2 to data_[row2][col2]
        for (int i = 0; i < row2; ++i) cur2 = cur2->down_;
        for (int j = 0; j < col2; ++j) cur2 = cur2->right_;

        auto cur1_bak = cur1;
        auto cur2_bak = cur2;

        // Swap the first row
        swap(cur1->val_, cur2->val_);
        for (int j = 1; j < width; ++j) {
            cur1 = cur1->right_;
            cur2 = cur2->right_;
            swap(cur1->val_, cur2->val_);
            if (height > 1) swap(cur1->down_, cur2->down_);
        }
        // Swap the last column
        if (width > 1) {
            for (int i = 1; i < height; ++i) {
                cur1 = cur1->down_;
                cur2 = cur2->down_;
                swap(cur1->right_, cur2->right_);
            }
        }

        // Return cur1 and cur2 to initial position
        cur1 = cur1_bak;
        cur2 = cur2_bak;

        // Swap the first column
        for (int i = 1; i < height; ++i) {
            cur1 = cur1->down_;
            cur2 = cur2->down_;
            swap(cur1->val_, cur2->val_);
            if (width > 1) swap(cur1->right_, cur2->right_);
        }
        // Swap the last row
        if (height > 1) {
            for (int j = 1; j < width; ++j) {
                cur1 = cur1->right_;
                cur2 = cur2->right_;
                swap(cur1->down_, cur2->down_);
            }
        }
    }

protected:
    vector<vector<Node*>> data_;
    int                   row_size_;
    int                   col_size_;
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    Matrix matrix(n, m);
    matrix.Init();
    for (int i = 0; i < q; ++i) {
        int a, b, c, d, h, w;
        cin >> a >> b >> c >> d >> h >> w;
        matrix.Swap(a, b, c, d, h, w);
        // cout << "Case " << i + 1 << ":\n";
        // matrix.Print();
        // cout << "\n";
    }
    matrix.Print();
    return 0;
}


#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::swap;
using std::vector;

class Node {
public:
    friend class Matrix;

    explicit Node(int val = 0, Node* right = nullptr, Node* down = nullptr)
        : val_(val), right_(right), down_(down) {}
    ~Node() {}

protected:
    int   val_;
    Node *right_, *down_;
};

class Matrix {
public:
    Matrix(int row_size, int col_size, int val = 0)
        : data_(row_size + 2, vector<Node*>(col_size + 2, nullptr)),
          row_size_(row_size),
          col_size_(col_size) {
        for (auto&& row : data_)
            for (auto&& i : row) i = new Node;
    }
    ~Matrix() {
        for (auto&& row : data_)
            for (auto&& i : row) delete i;
    }

    void Init() {
        for (int j = 0; j <= col_size_; ++j) {
            data_[0][j]->right_ = data_[0][j + 1];
            data_[0][j]->down_  = data_[1][j];
        }
        for (int i = 1; i <= row_size_; ++i) {
            data_[i][0]->right_ = data_[i][1];
            data_[i][0]->down_  = data_[i + 1][0];
        }
        for (int i = 1; i <= row_size_; ++i) {
            for (int j = 1; j <= col_size_; ++j) {
                int val;
                cin >> val;
                data_[i][j]->val_   = val;
                data_[i][j]->right_ = data_[i][j + 1];
                data_[i][j]->down_  = data_[i + 1][j];
            }
        }
    }

    void Print() {
        auto row_begin = data_[0][0];
        for (int i = 1; i <= row_size_; ++i) {
            if (!row_begin->down_) break;  // this statement shouldn't be run
            row_begin = row_begin->down_;
            auto cur  = row_begin;
            for (int j = 1; j <= col_size_; ++j) {
                if (!cur->right_) break;  // this statement shouldn't be run
                cur = cur->right_;
                cout << cur->val_ << (j == col_size_ ? '\n' : ' ');
            }
        }
    }

    void Swap(int row1, int col1, int row2, int col2, int height, int width) {
        auto cur1 = data_[0][0];
        auto cur2 = data_[0][0];

        // Move cur1 to data_[row1][col1]
        for (int i = 0; i < row1; ++i) cur1 = cur1->down_;
        for (int j = 0; j < col1; ++j) cur1 = cur1->right_;
        // Move cur2 to data_[row2][col2]
        for (int i = 0; i < row2; ++i) cur2 = cur2->down_;
        for (int j = 0; j < col2; ++j) cur2 = cur2->right_;

        auto cur1_bak = cur1;
        auto cur2_bak = cur2;

        // Swap the first row
        swap(cur1->val_, cur2->val_);
        for (int j = 1; j < width; ++j) {
            cur1 = cur1->right_;
            cur2 = cur2->right_;
            swap(cur1->val_, cur2->val_);
            if (height > 1) swap(cur1->down_, cur2->down_);
        }
        // Swap the last column
        if (width > 1) {
            for (int i = 1; i < height; ++i) {
                cur1 = cur1->down_;
                cur2 = cur2->down_;
                swap(cur1->right_, cur2->right_);
            }
        }

        // Return cur1 and cur2 to initial position
        cur1 = cur1_bak;
        cur2 = cur2_bak;

        // Swap the first column
        for (int i = 1; i < height; ++i) {
            cur1 = cur1->down_;
            cur2 = cur2->down_;
            swap(cur1->val_, cur2->val_);
            if (width > 1) swap(cur1->right_, cur2->right_);
        }
        // Swap the last row
        if (height > 1) {
            for (int i = 1; i < height; ++i) {
                cur1 = cur1->right_;
                cur2 = cur2->right_;
                swap(cur1->down_, cur2->down_);
            }
        }
    }

protected:
    vector<vector<Node*>> data_;
    int                   row_size_;
    int                   col_size_;
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    Matrix matrix(n, m);
    matrix.Init();
    for (int i = 0; i < q; ++i) {
        int a, b, c, d, h, w;
        cin >> a >> b >> c >> d >> h >> w;
        matrix.Swap(a, b, c, d, h, w);
    }
    matrix.Print();
    return 0;
}


#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

struct Node {
  Node *left, *right, *up, *down;
  int val;
};

const int N = 1e3 + 10;
Node a[N][N];

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i - 1 >= 0) a[i][j].up = &a[i - 1][j];
      if (i + 1 < N) a[i][j].down = &a[i + 1][j];
      if (j - 1 >= 0) a[i][j].left = &a[i][j - 1];
      if (j + 1 < N) a[i][j].right = &a[i][j + 1];
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j].val;
    }
  }
  while (q--) {
    int x1, y1, x2, y2, h, w;
    cin >> x1 >> y1 >> x2 >> y2 >> h >> w;
    Node* r1 = a[x1];
    while (y1--) r1 = r1->right;
    Node* r2 = a[x2];
    while (y2--) r2 = r2->right;
    // trace(r1->val, r2->val);

    Node *p1 = r1, *p2 = r2;
    for (int j = 0; j < w; ++j) {
      p1->up->down = p2;
      p2->up->down = p1;
      swap(p1->up, p2->up);
      p1 = p1->right; p2 = p2->right;
    }
    p1 = p1->left; p2 = p2->left;
    // trace(p1->val, p2->val);

    for (int i = 0; i < h; ++i) {
      r1->left->right = r2;
      r2->left->right = r1;
      swap(r1->left, r2->left);

      p1->right->left = p2;
      p2->right->left = p1;
      swap(p1->right, p2->right);

      r1 = r1->down; r2 = r2->down;
      p1 = p1->down; p2 = p2->down;
    }
    p1 = p1->up; p2 = p2->up;

    for (int j = 0; j < w; ++j) {
      p1->down->up = p2;
      p2->down->up = p1;
      swap(p1->down, p2->down);
      p1 = p1->left; p2 = p2->left;
    }
  }

  for (int i = 1; i <= n; ++i) {
    Node* r = a[i]->right;
    for (int j = 0; j < m; ++j) {
      cout << r->val << " \n"[j + 1 == m];
      r = r->right;
    }
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct node {
	node* nx[4];
	int v;
};

int n, m, q;
node a[1005][1005];

node* setaj(int x, int y) {
	auto it = &a[0][0];
	while (x--)
		it = it->nx[0];
	while (y--)
		it = it->nx[1];
	return it;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	cin >> n >> m >> q;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++) {
			cin >> a[i][j].v;
		}
	}
	n += 2;
	m += 2;

	for (int i=0; i<n-1; i++) {
		for (int j=0; j<m; j++) {
			a[i][j].nx[0] = &a[i+1][j];
			a[i+1][j].nx[2] = &a[i][j];
		}
	}

	for (int i=0; i<n; i++) {
		for (int j=0; j<m-1; j++) {
			a[i][j].nx[1] = &a[i][j+1];
			a[i][j+1].nx[3] = &a[i][j];
		}
	}

	while (q--) {
		int x, y, c, d, h, w;
		cin >> x >> y >> c >> d >> h >> w;

		auto it = setaj(x, y);
		auto jt = setaj(c, d);

		vector<pair<node**, node**>> sw;

		for (int dir=0; dir<4; dir++) {
			for (int t=0; t<(dir%2?w:h); t++) {
				int u = (dir+3) & 3, v = (dir+1) & 3;
				sw.emplace_back(&it->nx[u], &jt->nx[u]);
				sw.emplace_back(&it->nx[u]->nx[v], &jt->nx[u]->nx[v]);
				if (t < (dir%2?w:h)-1)
					it = it->nx[dir], jt = jt->nx[dir];
			}
		}

		for (auto [x, y] : sw)
			swap(*x, *y);
	}

	for (int i=1; i<=n-2; i++) {
		auto it = setaj(i, 1);
		for (int j=0; j<m-2; j++)
			cout << it->v << " \n"[j == m-3], it = it->nx[1];
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <list>
#include <deque>
using namespace std;

#define mp make_pair

const int N = 1e3 + 5;
typedef long long li;
const li INF = 1e16;
const int K = 32;

int mx[N][N];
int ans[N][N];
pair<int, int> r[N][N];
pair<int, int> down[N][N];
int n, m, q;

pair<int, int> real(pair<int, int> q){
	pair<int, int> t = mp(0, 0);
	for (int i = 0; i < q.first; i++){
		t = down[t.first][t.second];
	}
	for (int j = 0; j < q.second; j++){
		t = r[t.first][t.second];
	}
	return t;
}

int main(){
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf(" %d %d %d", &n, &m, &q);

	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			scanf(" %d", &mx[i][j]);
		}
	}

	for (int i = 0; i <= n; i++){
		for (int j = 0; j <= m; j++){
			r[i][j] = mp(i, j + 1);
			down[i][j] = mp(i + 1, j);
		}
	}

	for (int i = 0; i < q; i++){
		int a, b;
		int c, d;
		int h, w;
		scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &h, &w);

		pair<int, int> q1 = real(mp(a - 1, b));
		pair<int, int> q2 = real(mp(c - 1, d));
		pair<int, int> q3 = real(mp(a + h - 1, b));
		pair<int, int> q4 = real(mp(c + h - 1, d));
		for (int j = 0; j < w; j++){
			swap(down[q1.first][q1.second], down[q2.first][q2.second]);
			swap(down[q3.first][q3.second], down[q4.first][q4.second]);

			q1 = r[q1.first][q1.second];
			q2 = r[q2.first][q2.second];
			q3 = r[q3.first][q3.second];
			q4 = r[q4.first][q4.second];
		}

		pair<int, int> p1 = real(mp(a, b - 1));
		pair<int, int> p2 = real(mp(c, d - 1));
		pair<int, int> p3 = real(mp(a, b + w - 1));
		pair<int, int> p4 = real(mp(c, d + w - 1));
		for (int j = 0; j < h; j++){
			swap(r[p1.first][p1.second], r[p2.first][p2.second]);
			swap(r[p3.first][p3.second], r[p4.first][p4.second]);

			p1 = down[p1.first][p1.second];
			p2 = down[p2.first][p2.second];
			p3 = down[p3.first][p3.second];
			p4 = down[p4.first][p4.second];
		}
	}

	pair<int, int> t = mp(0, 0);
	for (int i = 0; i <= n; i++){
		pair<int, int> nt = t;
		for (int j = 0; j <= m; j++){
			ans[i][j] = mx[nt.first][nt.second];
			nt = r[nt.first][nt.second];
		}
		t = down[t.first][t.second];
	}

	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			printf("%d ", ans[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <fstream>

using namespace std;

inline void printMatrix(unsigned long **matrix,
	long row,
	long col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			cout << matrix[i][j] << ' ';
		}
		cout << endl;
	}
}

class Rect
{
public:
	Rect(unsigned long **matrix, long row, long col, long width, long height) :
		m_matrix(matrix),
		m_row(row),
		m_col(col),
		m_width(width),
		m_height(height),
		row(0),
		col(0)
	{
	}
	long totalCells() const {
		return m_width*m_height;
	}

	unsigned long& currValue()
	{
		return m_matrix[m_row+row][m_col+col];
	}

	void next()
	{
		col++;
		if (col == m_width)
		{
			col = 0;
			row++;
		}
	}
private:
	unsigned long **m_matrix;
	const long m_row;
	const long m_col;
	const long m_width;
	const long m_height;
	long row;
	long col;
};

int main() {
	long row, col, n_task;
	cin >> row >> col >> n_task;
	unsigned long **matrix = new unsigned long*[row];
	for (int i = 0; i < row; ++i)
		matrix[i] = new unsigned long[col];
	for (int i = 0; i < row; ++i)
		for (int j = 0; j < col; ++j)
			cin >> matrix[i][j];

	for (int i = 0; i < n_task; ++i)
	{
		long a, b, c, d, h, w;
		cin >> a >> b >> c >> d >> h >> w;
		Rect rect1(matrix, a - 1, b - 1, w, h);
		Rect rect2(matrix, c - 1, d - 1, w, h);
		for (int j = 0; j<rect1.totalCells(); j++)
		{
			unsigned long tmp = rect1.currValue();
			rect1.currValue() = rect2.currValue();
			rect2.currValue() = tmp;
			rect1.next();
			rect2.next();
		}
	}

	printMatrix(matrix, row, col);

	for (int i = 0; i < row; ++i) {
		delete[] matrix[i];
	}
	delete[] matrix;
	return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
#define M_SZ 1000

inline void printMatrix(unsigned long(*matrix)[M_SZ][M_SZ],
	long row,
	long col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			cout << (*matrix)[i][j] << ' ';
		}
		cout << endl;
	}
}

class Rect
{
public:
	Rect(unsigned long(*matrix)[M_SZ][M_SZ], long row, long col, long width, long height) :
		m_matrix(matrix),
		m_row(row),
		m_col(col),
		m_width(width),
		m_height(height),
		row(0),
		col(0)
	{
	}
	long totalCells() const {
		return m_width*m_height;
	}

	unsigned long& currValue()
	{
		return (*m_matrix)[m_row+row][m_col+col];
	}

	void next()
	{
		col++;
		if (col == m_width)
		{
			col = 0;
			row++;
		}
	}
private:
	unsigned long(*m_matrix)[M_SZ][M_SZ];
	const long m_row;
	const long m_col;
	const long m_width;
	const long m_height;
	long row;
	long col;
};

static unsigned long matrix[1000][1000];
static unsigned long buffer[1][1000];

int main() {
	long row, col, n_task;
	cin >> row >> col >> n_task;
	//unsigned long **matrix = new unsigned long*[row];
	//for (int i = 0; i < row; ++i)
	//	matrix[i] = new unsigned long[col];
	for (int i = 0; i < row; ++i)
		for (int j = 0; j < col; ++j)
			cin >> matrix[i][j];

	for (int i = 0; i < n_task; ++i)
	{
		long a, b, c, d, h, w;
		cin >> a >> b >> c >> d >> h >> w;
		a--;
		b--;
		c--;
		d--;
		//Rect rect1(&matrix, a - 1, b - 1, w, h);
		//Rect rect2(&matrix, c - 1, d - 1, w, h);
		//Копируем строками
		for (int j = 0; j<h; j++)
		{
			memcpy(&buffer[0][0], &matrix[a + j][b], sizeof(unsigned long)*w);
			memcpy(&matrix[a + j][b], &matrix[c + j][d], sizeof(unsigned long)*w);
			memcpy(&matrix[c + j][d], &buffer[0][0], sizeof(unsigned long)*w);
			//unsigned long tmp = rect1.currValue();
			//rect1.currValue() = rect2.currValue();
			//rect2.currValue() = tmp;
			//rect1.next();
			//rect2.next();
		}
	}

	printMatrix(&matrix, row, col);

	//for (int i = 0; i < row; ++i) {
	//	delete[] matrix[i];
	//}
	//delete[] matrix;
	return 0;
}


#include <iostream>
#include <fstream>

using namespace std;

inline void printMatrix(unsigned long **matrix,
	long row,
	long col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			cout << matrix[i][j] << ' ';
		}
		cout << endl;
	}
}

class Rect
{
public:
	Rect(unsigned long **matrix, long row, long col, long width, long height) :
		m_matrix(matrix),
		m_row(row),
		m_col(col),
		m_width(width),
		m_height(height)
	{
	}
	long totalCells() const {
		return m_width*m_height;
	}

	unsigned long& operator[](long idx)
	{
		long row = idx / m_height;
		if ((m_height == 1) && (idx == 1)) row = 0;
		long col = idx % m_width;
		if ((m_width == 1) && (idx == 1)) col = 0;
		return m_matrix[m_row+row][m_col+col];
	}
private:
	unsigned long **m_matrix;
	const long m_row;
	const long m_col;
	const long m_width;
	const long m_height;
};

int main() {
	long row, col, n_task;
	cin >> row >> col >> n_task;
	unsigned long **matrix = new unsigned long*[row];
	for (int i = 0; i < row; ++i)
		matrix[i] = new unsigned long[col];
	for (int i = 0; i < row; ++i)
		for (int j = 0; j < col; ++j)
			cin >> matrix[i][j];

	for (int i = 0; i < n_task; ++i)
	{
		long a, b, c, d, h, w;
		cin >> a >> b >> c >> d >> h >> w;
		Rect rect1(matrix, a - 1, b - 1, w, h);
		Rect rect2(matrix, c - 1, d - 1, w, h);
		for (int j = 0; j<rect1.totalCells(); j++)
		{
			unsigned long tmp = rect1[j];
			rect1[j] = rect2[j];
			rect2[j] = tmp;
		}
	}

	printMatrix(matrix, row, col);

	for (int i = 0; i < row; ++i) {
		delete[] matrix[i];
	}
	delete[] matrix;
	return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
#define M_SZ 1000

inline void printMatrix(unsigned long(*matrix)[M_SZ][M_SZ],
	long row,
	long col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			cout << (*matrix)[i][j] << ' ';
		}
		cout << endl;
	}
}

class Rect
{
public:
	Rect(unsigned long(*matrix)[M_SZ][M_SZ], long row, long col, long width, long height) :
		m_matrix(matrix),
		m_row(row),
		m_col(col),
		m_width(width),
		m_height(height),
		row(0),
		col(0)
	{
	}
	long totalCells() const {
		return m_width*m_height;
	}

	unsigned long& currValue()
	{
		return (*m_matrix)[m_row+row][m_col+col];
	}

	void next()
	{
		col++;
		if (col == m_width)
		{
			col = 0;
			row++;
		}
	}
private:
	unsigned long(*m_matrix)[M_SZ][M_SZ];
	const long m_row;
	const long m_col;
	const long m_width;
	const long m_height;
	long row;
	long col;
};

static unsigned long matrix[1000][1000];

int main() {
	long row, col, n_task;
	cin >> row >> col >> n_task;
	//unsigned long **matrix = new unsigned long*[row];
	//for (int i = 0; i < row; ++i)
	//	matrix[i] = new unsigned long[col];
	for (int i = 0; i < row; ++i)
		for (int j = 0; j < col; ++j)
			cin >> matrix[i][j];

	for (int i = 0; i < n_task; ++i)
	{
		long a, b, c, d, h, w;
		cin >> a >> b >> c >> d >> h >> w;
		Rect rect1(&matrix, a - 1, b - 1, w, h);
		Rect rect2(&matrix, c - 1, d - 1, w, h);
		for (int j = 0; j<rect1.totalCells(); j++)
		{
			unsigned long tmp = rect1.currValue();
			rect1.currValue() = rect2.currValue();
			rect2.currValue() = tmp;
			rect1.next();
			rect2.next();
		}
	}

	printMatrix(&matrix, row, col);

	//for (int i = 0; i < row; ++i) {
	//	delete[] matrix[i];
	//}
	//delete[] matrix;
	return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <sstream>
#include <assert.h>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <complex>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <map>

using namespace std;

const int N = 1111;

struct Cell
{
	Cell* right, *down;
	int value;
};

Cell* a[N][N];

int n, m, q;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 0; i <= n + 1; ++i)
	{
		for (int j = 0; j <= m + 1; ++j)
		{
			a[i][j] = new Cell();
		}
	}
	a[0][0]->right = a[0][1];
	a[0][0]->down = a[1][0];
	for (int i = 1; i <= n; ++i)
	{
		a[i][0]->right = a[i][1];
		a[i][0]->down = a[i + 1][0];
	}
	for (int i = 1; i <= m; ++i)
	{
		a[0][i]->down = a[1][i];
		a[0][i]->right = a[0][i + 1];
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			int c; scanf("%d", &c);
			a[i][j]->value = c;
			a[i][j]->right = a[i][j + 1];
			a[i][j]->down = a[i + 1][j];
		}
	}
	for (int i = 0; i < q; ++i)
	{
		int ax, ay, bx, by, h, w; scanf("%d%d%d%d%d%d", &ay, &ax, &by, &bx, &h, &w);
		Cell* p1 = a[ay - 1][0];
		Cell* p2 = a[by - 1][0];
		Cell* p3 = a[ay][0];
		Cell* p4 = a[by][0];
		Cell* p5 = a[ay][0];
		Cell* p6 = a[by][0];
		Cell* p7 = a[ay + h - 1][0];
		Cell* p8 = a[by + h - 1][0];
		for (int j = 0; j < ax; ++j)
		{
			p1 = p1->right;
			p7 = p7->right;
		}
		for (int j = 0; j < bx; ++j)
		{
			p2 = p2->right;
			p8 = p8->right;
		}
		for (int j = 0; j < ax - 1; ++j)
		{
			p3 = p3->right;
		}
		for (int j = 0; j < bx - 1; ++j)
		{
			p4 = p4->right;
		}
		for (int j = 0; j < ax + w - 1; ++j)
		{
			p5 = p5->right;
		}
		for (int j = 0; j < bx + w - 1; ++j)
		{
			p6 = p6->right;
		}
		for (int j = 0; j < w; ++j)
		{
			swap(p1->down, p2->down);
			swap(p7->down, p8->down);
			p1 = p1->right;
			p2 = p2->right;
			p7 = p7->right;
			p8 = p8->right;
		}
		for (int j = 0; j < h; ++j)
		{
			swap(p3->right, p4->right);
			swap(p5->right, p6->right);
			p3 = p3->down;
			p4 = p4->down;
			p5 = p5->down;
			p6 = p6->down;
		}

	}
	for (int i = 1; i <= n; ++i)
	{
		Cell* ans = a[i][0];
		for (int j = 0; j < m; ++j)
		{
			ans = ans->right;
			printf("%d ", ans->value);
		}
		printf("\n");
	}
	return 0;
}