def rotate r, c, size, angle
    temp = []
    size.times{|c1| temp << @matrix[r-1+c1][c-1, size].dup}
    ((angle / 90) % 4).times{
        temp = temp.transpose
        temp.map!{|line| line.reverse}
    }
    size.times{|c1| @matrix[r-1+c1][c-1, size] = temp[c1]}
end

def reversal r, c, size
    size.times{|c1| size.times{|c2| @matrix[r-1+c1][c-1+c2] = 1-@matrix[r-1+c1][c-1+c2]}}
end

def leftshift r
    @matrix[r-1].rotate! 1
end

def rightshift r
    @matrix[r-1].rotate! -1
end

def islandreversal r, c
    x = @matrix[r-1][c-1]
    temp = [[r-1,c-1]]
    until temp.empty?
        temp2 = temp.pop
        if @matrix[temp2[0]][temp2[1]] == x
            @matrix[temp2[0]][temp2[1]] = 1 - @matrix[temp2[0]][temp2[1]]
            temp << [temp2[0]-1, temp2[1]] if temp2[0] > 0
            temp << [temp2[0]+1, temp2[1]] if temp2[0] < @n-1
            temp << [temp2[0], temp2[1]-1] if temp2[1] > 0
            temp << [temp2[0], temp2[1]+1] if temp2[1] < @n-1
        end
    end
end

@n, m = gets.chomp.split.map(&:to_i)
@matrix = []
@n.times{@matrix << gets.chomp.split.map(&:to_i)}
m.times{
    op, r, c, size, angle = gets.chomp.split.map(&:to_i)
    case op
    when 0 then rotate(r, c, size, angle)
    when 1 then reversal(r, c, size)
    when 2 then leftshift(r)
    when 3 then rightshift(r)
    when 4 then islandreversal(r, c)
    end
}
@matrix.each{|row| puts row.join(" ")}

