def max_sum_rectangular_grid(grid, n):
    if n == 0:
        return 0
    if n == 1:
        return max(grid[0][0], grid[1][0])
    
    # dp[i] = max sum considering columns 0..i
    # At each column, we can pick one or both cells, or neither
    # But no two chosen numbers can be adjacent (horizontally or vertically)
    
    # For a 2xn grid:
    # Adjacent means: same row neighboring columns, or same column different rows
    # So within a column, the two cells are adjacent to each other
    # Between columns, cells in same row are adjacent
    
    # States for each column:
    # 0: neither cell chosen
    # 1: top cell chosen
    # 2: bottom cell chosen
    # (can't choose both in same column as they're adjacent)
    
    # dp[state] = max sum ending with given state at current column
    
    INF = float('-inf')
    
    # Initialize for column 0
    dp = [0, grid[0][0], grid[1][0]]
    # dp[0] = 0 (neither), dp[1] = grid[0][0] (top), dp[2] = grid[1][0] (bottom)
    
    for j in range(1, n):
        new_dp = [INF, INF, INF]
        top = grid[0][j]
        bot = grid[1][j]
        
        # new state 0: pick neither in column j
        # previous state can be anything
        new_dp[0] = max(dp[0], dp[1], dp[2])
        
        # new state 1: pick top in column j
        # previous state cannot be 1 (same row adjacent)
        # previous state 0 or 2 is ok
        new_dp[1] = max(dp[0], dp[2]) + top
        
        # new state 2: pick bottom in column j
        # previous state cannot be 2 (same row adjacent)
        # previous state 0 or 1 is ok
        new_dp[2] = max(dp[0], dp[1]) + bot
        
        dp = new_dp
    
    return max(dp)