## 562. Longest Line of Consecutive One in Matrix

Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.

Example:

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

Hint: The number of elements in the given matrix will not exceed 10,000.

## Rust Solution

``````struct Solution;

impl Solution {
fn longest_line(matrix: Vec<Vec<i32>>) -> i32 {
let n = matrix.len();
if n == 0 {
return 0;
}
let m = matrix[0].len();
let mut dp: Vec<Vec<Vec<i32>>> = vec![vec![vec![0; 4]; m]; n];
let mut res = 0;
for i in 0..n {
for j in 0..m {
if matrix[i][j] == 1 {
dp[i][j][0] += 1;
dp[i][j][1] += 1;
dp[i][j][2] += 1;
dp[i][j][3] += 1;
if i > 0 {
dp[i][j][0] += dp[i - 1][j][0];
}
if j > 0 {
dp[i][j][1] += dp[i][j - 1][1];
}
if i > 0 && j > 0 {
dp[i][j][2] += dp[i - 1][j - 1][2];
}
if i > 0 && j + 1 < m {
dp[i][j][3] += dp[i - 1][j + 1][3];
}
res = res.max(*dp[i][j].iter().max().unwrap());
}
}
}
res
}
}

#[test]
fn test() {
let matrix = vec_vec_i32![[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1]];
let res = 3;
assert_eq!(Solution::longest_line(matrix), res);
}
``````

Having problems with this solution? Click here to submit an issue on github.