### subroutines

def solve(n, mat, v)
  for i in (0...n)
    if mat[i][i] == 0
      for i0 in ((i + 1)...n)
        if mat[i0][i] != 0
          tmp = mat[i0]; mat[i0] = mat[i]; mat[i] = tmp
          tmp = v[i0]; v[i0] = v[i]; v[i] = tmp
          break
        end
      end
    end

    if mat[i][i] != 0
      for i0 in ((i + 1)...n)
        if mat[i0][i] != 0
          mat[i0][i] = 0
          for j in ((i + 1)...n)
            mat[i0][j] = (mat[i0][j] + mat[i][j]) % 2
          end
          v[i0] = (v[i0] + v[i]) % 2
        end
      end
    end
  end
  #p mat; p v
  
  (n - 1).downto(0) do |i|
    ((i + 1)...n).each{|j| v[i] = (v[i] + mat[i][j] * v[j]) % 2}
    return false if mat[i][i] == 0 && v[i] != 0
  end
  #p mat; p v
  
  true
end

### main

loop do
  w, h, d = gets.split.map(&:to_i)
  break if (w | h | d) == 0

  hw = h * w
  v = []
  mat = []

  for y in (0...h)
    line = gets.split.map(&:to_i)
    for x in (0...w)
      pos = y * w + x
      v[pos] = line[x]

      mat[pos] = hw.times.map{0}
      mat[pos][pos] = 1
      
      for dy in (-d..d)
        ddx = d - dy.abs
        for dx in [-ddx, ddx]
          x0 = x + dx
          y0 = y + dy
          if x0 >= 0 && x0 < w && y0 >= 0 && y0 < h
            pos0 = y0 * w + x0
            mat[pos][pos0] = 1
          end
        end
      end
    end
  end
  #p v
  #p mat

  puts (solve(hw, mat, v) ? 1 : 0)
end