Medium · Matrices

Longest Line of Consecutive One in Matrix

Given an m × n binary matrix mat, return the length of the longest line of consecutive 1s, where a line may run horizontally, vertically, diagonally or anti-diagonally; return 0 if the matrix has no 1.

Examples

Example 1

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

Output: longest line = 3

Example 2

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

Output: longest line = 3

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