require 'set'

loop do
    n = gets.to_i
    break if n == 0

    schedule = (1..n).map {gets.split.map(&:to_i)}

    if schedule[0][5] == 1 || schedule[0][6] == 1 || schedule[0][9] == 1 || schedule[0][10] == 1
        p 0
        next
    end

    state = [1] * 16 + [1, 1]
    state[5] = state[6] = state[9] = state[10] = 0
    states = [state]

    (1..n-1).each do |k|
        next_states = Set.new
        states.each do |*a, i, j|
            # p [a, i, j]
            (0..2).to_a.repeated_permutation(2) do |y, x|
                next if i != y && j != x
                next if schedule[k][y*4+x] == 1
                next if schedule[k][y*4+x+1] == 1
                next if schedule[k][(y+1)*4+x] == 1
                next if schedule[k][(y+1)*4+x+1] == 1
                b = a.map {|t| t + 1}
                b[y*4+x] = 0
                b[y*4+x+1] = 0
                b[(y+1)*4+x] = 0
                b[(y+1)*4+x+1] = 0
                next if b.any? {|t| t > 6}
                next_states << [*b, y, x]
            end
        end
        states = next_states
    end

    p states.empty? ? 0 : 1
end