Set up top row and left column
for i in range ( 1 , N ) : NEW_LINE INDENT if grid [ 0 ] [ i ] == 1 : NEW_LINE INDENT grid [ 0 ] [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ 0 ] [ i ] = grid [ 0 ] [ i - 1 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT if grid [ i ] [ 0 ] == 1 : NEW_LINE INDENT grid [ i ] [ 0 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ i ] [ 0 ] = grid [ i - 1 ] [ 0 ] + 1 NEW_LINE DEDENT DEDENT