> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Longest Line of Consecutive One in Matrix

> Tested Python solution for LeetCode 562 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 562, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 562   # by problem number
lcpy gen -s longest_line_of_consecutive_one_in_matrix   # by problem name
```

## Problem

Given an \<code>m x n\</code> binary matrix \<code>mat\</code>, return \<em>the length of the longest line of consecutive one in the matrix\</em>.

The line could be horizontal, vertical, diagonal, or anti-diagonal.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0562.Longest%20Line%20of%20Consecutive%20One%20in%20Matrix/images/long1-grid.jpg)

```
Input: mat = [[0,1,1,0],[0,1,1,0],[0,0,0,1]]
Output: 3
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0562.Longest%20Line%20of%20Consecutive%20One%20in%20Matrix/images/long2-grid.jpg)

```
Input: mat = [[1,1,1,1],[0,1,1,0],[0,0,0,1]]
Output: 4
```

### Constraints

* `m == mat.length`
* `n == mat[i].length`
* `1 <= m, n <= 10^4`
* `1 <= m * n <= 10^4`
* `mat[i][j]` is either `0` or `1`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_line_of_consecutive_one_in_matrix/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_line_of_consecutive_one_in_matrix/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(n)
    def longest_line(self, mat: list[list[int]]) -> int:
        n = len(mat[0])
        prev_vertical = [0] * n
        prev_diagonal = [0] * n
        prev_anti_diagonal = [0] * n
        best = 0
        for row in mat:
            cur_vertical = [0] * n
            cur_diagonal = [0] * n
            cur_anti_diagonal = [0] * n
            horizontal = 0
            for j, value in enumerate(row):
                if value == 1:
                    horizontal += 1
                    cur_vertical[j] = prev_vertical[j] + 1
                    cur_diagonal[j] = prev_diagonal[j - 1] + 1 if j > 0 else 1
                    cur_anti_diagonal[j] = prev_anti_diagonal[j + 1] + 1 if j + 1 < n else 1
                    best = max(
                        best, horizontal, cur_vertical[j], cur_diagonal[j], cur_anti_diagonal[j]
                    )
                else:
                    horizontal = 0
            prev_vertical = cur_vertical
            prev_diagonal = cur_diagonal
            prev_anti_diagonal = cur_anti_diagonal
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.