require 'set'
n, m = gets.split.map(&:to_i)
a = (1..n).map { gets.split.map(&:to_i) }
m.times do
    o = gets.split.map(&:to_i)
    if o[0] == 0
        i0, j0, s, t = o[1]-1, o[2]-1, o[3], o[4]
        r1 = (i0..i0+s-1)
        r2 = (j0..j0+s-1)
        b = r1.map {|i| r2.map {|j| a[i][j]}}
        (t / 90).times do
            b = (0..s-1).map{|i| (0..s-1).map{|j| b[s-1-j][i]}}
        end
        a = (0..n-1).map {|i| (0..n-1).map {|j|
            if (i0..i0+s-1).include?(i) && (j0..j0+s-1).include?(j)
                b[i-i0][j-j0]
            else
                a[i][j]
            end
        }}
    elsif o[0] == 1
        i0, j0, s = o[1]-1, o[2]-1, o[3]
        a = (0..n-1).map {|i| (0..n-1).map {|j|
            if (i0..i0+s-1).include?(i) && (j0..j0+s-1).include?(j)
                1 - a[i][j]
            else
                a[i][j]
            end
        }}
    elsif o[0] == 2
        a[o[1]-1] << a[o[1]-1].shift
    elsif o[0] == 3
        a[o[1]-1].unshift a[o[1]-1].pop
    elsif o[0] == 4
        queue = [[o[1]-1, o[2]-1]]
        reached = Set[*queue]
        until queue.empty?
            i, j = queue.shift
            [[i-1, j], [i+1, j], [i, j-1], [i, j+1]].each do |t|
                if t.min >= 0 && t.max < n && a[t[0]][t[1]] == a[i][j] && !reached.include?(t)
                    queue << t
                    reached << t
                end
            end
        end
        reached.each do |i, j|
            a[i][j] = 1 - a[i][j]
        end
    end
end
a.each do |r|
    puts r * ' '
end
