### constant

H = W = 4
HW = H * W

CH = H - 1
CW = W - 1
CHW = CH * CW

MAX_R = 6

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

### subroutines

def check_rec(k, cid, r0, r2, r6, r8)
  return true if k >= $n
  return $cache[k][cid][r0][r2][r6][r8] if $cache[k][cid][r0][r2][r6][r8]

  for pos in $ccvrs[cid]
    return ($cache[k][cid][r0][r2][r6][r8] = false) if $fcs[k][pos]
  end

  rr0 = r0 + 1
  rr2 = r2 + 1
  rr6 = r6 + 1
  rr8 = r8 + 1

  case cid
  when 0
    rr0 = 0
  when 2
    rr2 = 0
  when 6
    rr6 = 0
  when 8
    rr8 = 0
  end

  if rr0 > MAX_R || rr2 > MAX_R || rr6 > MAX_R || rr8 > MAX_R
    return ($cache[k][cid][r0][r2][r6][r8] = false)
  end

  for cid0 in $cnbrs[cid]
    if check_rec(k + 1, cid0, rr0, rr2, rr6, rr8)
      return ($cache[k][cid][r0][r2][r6][r8] = true)
    end
  end

  ($cache[k][cid][r0][r2][r6][r8] = false)
end

### main

$ccvrs = []
$cnbrs = CHW.times.map{[]}

for y in (0...CH)
  for x in (0...CW)
    cid = y * CW + x
    cpos = y * W + x
    $ccvrs[cid] = [cpos, cpos + 1, cpos + W, cpos + W + 1]

    DXYS.each do |dx, dy|
      x0 = x + dx
      y0 = y + dy
      if x0 >= 0 && x0 < CW && y0 >= 0 && y0 < CH
        cid0 = y0 * CW + x0
        $cnbrs[cid] << cid0
      end
    end
    $cnbrs[cid].sort!
  end
end
#p $ccvrs
#p $cnbrs

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

  $fcs = $n.times.map{gets.split.map{|c| c == '1'}}
  #p $fcs

  $cache = $n.times.map{CHW.times.map{(MAX_R + 1).times.map{(MAX_R + 1).times.map{(MAX_R + 1).times.map{(MAX_R + 1).times.map{false}}}}}}

  puts check_rec(0, 4, 0, 0, 0, 0) ? 1 : 0
end