Find every rectangular farmland group in a binary grid and return the coordinates of its top-left and bottom-right corners.
You are given a 2D binary grid where 1 represents farmland and 0 represents empty land. Each connected group of farmland forms one solid axis-aligned rectangle, and different rectangles do not touch each other horizontally or vertically.
Your task is to identify every farmland group and return the coordinates of its top-left and bottom-right cells.
Return the result in any order.
Input Format
- A 2D integer grid
landof sizem x n. land[i][j]is either0or1.- Each group of
1s forms a single rectangle.
Output Format
- Return a list of rectangles.
- For each rectangle, output
[topRow, leftCol, bottomRow, rightCol]. - The rectangles may be returned in any order.
Constraints
1 <= m, n <= 300land[i][j]is0or1- Farmland groups are disjoint and each is a perfect rectangle.
Example 1
Input
land = [[1,0,0],[0,1,1],[0,1,1]]
Output
[[0,0,0,0],[1,1,2,2]]
Explanation
There are two farmland rectangles: one single cell at the top-left, and one 2x2 block in the bottom-right.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.