```
public class Solution {
public int longestLine(int[][] M) {
int m = M.length, ones = 0;
if (m > 0) {
int n = M[0].length;
int[][] old = new int[4][n];
for (int i = 0; i < m; i++) {
int[][] curr = new int[4][n];
for (int j = 0; j < n; j++) {
if (M[i][j] == 1) {
curr[0][j] = ((j > 0) ? curr[0][j - 1] + 1 : 1);
curr[1][j] = ((i > 0) ? old[1][j] + 1 : 1);
curr[2][j] = ((j < n - 1) ? old[2][j + 1] + 1 : 1);
curr[3][j] = ((j > 0) ? old[3][j - 1] + 1 : 1);
ones = Math.max(ones, Math.max(Math.max(curr[0][j], curr[1][j]), Math.max(curr[2][j], curr[3][j])));
} else {
curr[0][j] = curr[1][j] = curr[2][j] = curr[3][j] = 0;
}
}
old = curr;
}
}
return ones;
}
}
```