@SuppressWarnings("unchecked")
	void run(){
		Scanner sc = new Scanner(System.in);
		for(;;){
			n = sc.nextInt();
			if(n==0)break;
			sun = new boolean[n][16];
			for(int i=0;i<n;i++)for(int j=0;j<16;j++)sun[i][j] = sc.nextInt()==1;
			set = new HashSet[n][9];
			for(int i=0;i<n;i++)for(int j=0;j<9;j++)set[i][j] = new HashSet<Long>();
			int[] a = new int[16];
			Arrays.fill(a, 5);
			if(!ok(0, 4)){
				System.out.println(0);continue;
			}
			for(int j=0;j<4;j++)a[cover[4][j]] = 6;
			System.out.println(get(0, 4, a)?1:0);
		}
	}