# include <cstdio>
# include <cstring>
# include <queue>
# include <vector>
using namespace std;
const int N = 366 ;
const int dx[ ] = { - 1 , 0 , 1 , 0 , 0 } , dy[ ] = { 0 , 1 , 0 , - 1 , 0 } ;
int n;
bool st[ N] [ 3 ] [ 3 ] [ 7 ] [ 7 ] [ 7 ] [ 7 ] ;
int state[ N] [ 4 ] [ 4 ] ;
struct Node
{
int day, x, y, s0, s1, s2, s3;
} ;
int bfs ( )
{
if ( state[ 1 ] [ 1 ] [ 1 ] || state[ 1 ] [ 1 ] [ 2 ] || state[ 1 ] [ 2 ] [ 1 ] || state[ 1 ] [ 2 ] [ 2 ] ) return 0 ;
queue< Node> q;
memset ( st, 0 , sizeof st) ;
q. push ( ( Node) { 1 , 1 , 1 , 1 , 1 , 1 , 1 } ) ;
st[ 1 ] [ 1 ] [ 1 ] [ 1 ] [ 1 ] [ 1 ] [ 1 ] = true ;
while ( q. size ( ) )
{
Node t= q. front ( ) ; q. pop ( ) ;
if ( t. day== n) return 1 ;
for ( int i= 0 ; i< 5 ; i++ )
for ( int j= 1 ; j<= 2 ; j++ )
{
int x= t. x+ dx[ i] * j, y= t. y+ dy[ i] * j;
if ( x< 0 || x>= 3 || y< 0 || y>= 3 ) continue ;
if ( state[ t. day+ 1 ] [ x] [ y] || state[ t. day+ 1 ] [ x] [ y+ 1 ] || state[ t. day+ 1 ] [ x+ 1 ] [ y] || state[ t. day+ 1 ] [ x+ 1 ] [ y+ 1 ] )
continue ;
int s0= t. s0, s1= t. s1, s2= t. s2, s3= t. s3;
if ( ! x&& ! y) s0= 0 ;
else if ( ++ s0== 7 ) continue ;
if ( ! x&& y== 2 ) s1= 0 ;
else if ( ++ s1== 7 ) continue ;
if ( x== 2 && ! y) s2= 0 ;
else if ( ++ s2== 7 ) continue ;
if ( x== 2 && y== 2 ) s3= 0 ;
else if ( ++ s3== 7 ) continue ;
if ( st[ t. day+ 1 ] [ x] [ y] [ s0] [ s1] [ s2] [ s3] ) continue ;
st[ t. day+ 1 ] [ x] [ y] [ s0] [ s1] [ s2] [ s3] = true ;
q. push ( ( Node) { t. day+ 1 , x, y, s0, s1, s2, s3} ) ;
}
}
return 0 ;
}
int main ( )
{
while ( scanf ( "%d" , & n) , n)
{
for ( int i= 1 ; i<= n; i++ )
for ( int j= 0 ; j< 4 ; j++ )
for ( int k= 0 ; k< 4 ; k++ )
scanf ( "%d" , & state[ i] [ j] [ k] ) ;
printf ( "%d\n" , bfs ( ) ) ;
}
return 0 ;
}