Medium · Matrices

Max region

Given an m × n binary matrix mat, return the number of cells in its largest region of 1s, where two 1s are in the same region if they touch horizontally, vertically or diagonally; return 0 if there is no 1.

Examples

Example 1

{
  "mat": [
    [1, 1, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 1, 0],
    [1, 0, 0, 1]
  ]
}

Output: max region = 6

Example 2

{
  "mat": [
    [1, 0, 0, 1],
    [1, 0, 1, 1],
    [0, 0, 0, 1]
  ]
}

Output: max region = 4

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Matrices problems