If the grid contains only 0 s or only 1 s
if ( len ( q ) == 0 or M * N == len ( q ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( len ( q ) > 0 ) : NEW_LINE INDENT cnt = len ( q ) NEW_LINE while ( cnt > 0 ) : NEW_LINE DEDENT