### constant

DXYS = [[1, 0], [0, 1], [-1, 0], [0, -1]]

### subroutines

def rotate(mat, r, c, size, angle)
  tmat = Array.new(size){Array.new(size)}

  while angle > 0
    for dy in (0...size)
      for dx in (0...size)
        tmat[dx][size - 1 - dy] = mat[r + dy][c + dx]
      end
    end
    for dy in (0...size)
      for dx in (0...size)
        mat[r + dy][c + dx] = tmat[dy][dx]
      end
    end
    angle -= 90
  end

  mat
end

def reverse(mat, r, c, size)
  for dy in (0...size)
    for dx in (0...size)
      mat[r + dy][c + dx] ^= 1
    end
  end
  mat
end

def lshift(mat, r)
  n = mat[r].length
  tmp = mat[r][0]
  for i in (0...(n - 1))
    mat[r][i] = mat[r][i + 1]
  end
  mat[r][n - 1] = tmp
  mat
end

def rshift(mat, r)
  n = mat[r].length
  tmp = mat[r][n - 1]
  (n - 1).downto(1) do |i|
    mat[r][i] = mat[r][i - 1]
  end
  mat[r][0] = tmp
  mat
end

def paint(mat, r, c)
  n = mat.length
  d = mat[r][c]
  mat[r][c] ^= 1
  q = [[c, r]]

  while ! q.empty?
    ux, uy = q.shift
    DXYS.each do |dx, dy|
      vx = ux + dx
      vy = uy + dy
      if vx >= 0 && vx < n && vy >= 0 && vy < n && mat[vy][vx] == d
        mat[vy][vx] ^= 1
        q << [vx, vy]
      end
    end
  end

  mat
end

### main

n, m = gets.split.map(&:to_i)

mat = Array.new(n)

for y in (0...n)
  mat[y] = gets.split.map(&:to_i)
end
#p mat

m.times do
  ops = gets.split.map(&:to_i)

  case ops[0]
  when 0
    mat = rotate(mat, ops[1] - 1, ops[2] - 1, ops[3], ops[4])
  when 1
    mat = reverse(mat, ops[1] - 1, ops[2] - 1, ops[3])
  when 2
    mat = lshift(mat, ops[1] - 1)
  when 3
    mat = rshift(mat, ops[1] - 1)
  when 4
    mat = paint(mat, ops[1] - 1, ops[2] - 1)
  end
  #p mat
end

for cols in mat
  puts cols.join(' ')
end