import std.stdio;
import std.algorithm;
import std.functional;
import std.conv;
import std.array;
import std.range;
import std.string;

int[][] rotate(int[][] A) {
    ulong N = A.length;
    int[][] B = new int[][](N, N);
    foreach (i; 0 .. N) {
        foreach (j; 0 .. N) {
            B[j][N - 1 - i] = A[i][j];
        }
    }
    return B;
}

void islandReverse(int[][] F, int r, int c) {
    ulong N = F.length;
    bool[][] used = new bool[][](N, N);
    void dfs(int y, int x) {
        static const dy = [0, 1, 0, -1],
                     dx = [1, 0, -1, 0];
        used[y][x] = true;
        foreach (i; 0 .. 4) {
            int ny = y + dy[i],
                nx = x + dx[i];
            if (ny < 0 || ny >= N) continue;
            if (nx < 0 || nx >= N) continue;
            if (used[ny][nx]) continue;
            if (F[y][x] == F[ny][nx]) 
                dfs(ny, nx);
        }
        F[y][x] = !F[y][x];
    }
    dfs(r, c);
}

int[][] rotateN(int[][] A, int n) {
    int[][] B = A.subArea(0, 0, A.length);
    foreach (i; 0 .. n) {
        B = B.rotate;
    }
    return B;
}

int[][] subArea(int[][] F, int r, int c, ulong size) {
    int[][] area = new int[][](size, size);
    foreach (i; 0 .. size) {
        foreach (j; 0 .. size) {
            area[i][j] = F[r + i][c + j];
        }
    }
    return area;
}

void copy(int[][] F, int r, int c, int[][] A) {
    ulong N = A.length;
    foreach (i; 0 .. N) {
        foreach (j; 0 .. N) {
            F[r + i][c + j] = A[i][j];
        }
    }
}

void dump(int[][] F) {
    foreach (ref L; F) {
        L.writeln;
    }
}

void reverse(int[][] F, int r, int c, ulong size) {
    foreach (i; 0 .. size) {
        foreach (j; 0 .. size) {
            F[r + i][c + j] = !F[r + i][c + j];
        }
    }
}

int[] shiftL(int[] L) {
    return L[1 .. $] ~ L[0];
}

int[] shiftR(int[] L) {
    return [L[$ - 1]] ~ L[0 .. $ - 1];
}

void main() {
    int n, m; readf("%d %d\n", &n, &m);
    int[][] F = new int[][n];
    foreach (i; 0 .. n) {
        F[i] = readln.chomp.split(" ").map!(to!int).array;
    }
    foreach (i; 0 .. m) {
        int[] o = readln.chomp.split(" ").map!(to!int).array;
        //F.dump; writeln("");
        switch (o[0]) {
            case 0: // Rotate
                int r = o[1] - 1,
                    c = o[2] - 1, 
                    size = o[3], 
                    angle = o[4];
                auto area = F.subArea(r, c, size);
                F.copy(r, c, area.rotateN(angle / 90));
                break;
            case 1: // Reversal
                int r = o[1] - 1,
                    c = o[2] - 1,
                    size = o[3];
                F.reverse(r, c, size);
                break;
            case 2: // Left shift
                int r = o[1] - 1;
                F[r] = F[r].shiftL;
                break;
            case 3: // Right shift
                int r = o[1] - 1;
                F[r] = F[r].shiftR;
                break;
            case 4: // Island reverse
                int r = o[1] - 1,
                    c = o[2] - 1;
                F.islandReverse(r, c);
                break;
            default:
                throw new Error("?");
        }
    }
    foreach (i; 0 .. n) {
        F[i].map!(to!string).join(" ").writeln;
    }
}

Measured memory_bytes: 1240