// Check the neighbor to the right (c+1) if (c + 1 < grid[r].length) // Safe to access grid[r][c+1]

Set every element in the grid to zero.

: This is the standard way to traverse a 2D array in Java. You visit every element in the first row, then every element in the second, and so on. Nested Loops : The outer loop typically controls the row index ( i ). The inner loop typically controls the column index ( j ). Implementation Guide 1. Standard Traversal Template

Fix the column index c and loop through all r . 3. Transforming the Grid Flipping and Reversing

return result;

int[][] grid = 1, 2, 3, 4, 5, 6, 7, 8, 9 ;

Imagine you are at grid[r][c] .