Four directions, which represents `horizontal`

, `vertical`

, `diagonal`

, `anti-diagonal`

.

basic idea is to only count a line if it is a start point. A start point could either be in the boundary or previous node in the direction is a zero.

```
public class Solution {
private static int[][] dirs = {{0, 1}, {1, 0}, {1, 1}, {1, -1}};
private boolean isBoundary(int type, int i, int j, int maxRight) {
if ((type == 0 && (j == 0)) ||
(type == 1 && (i == 0)) ||
(type == 2 && (i == 0 || j == 0)) ||
(type == 3 && (i == 0 || j == maxRight))) {
return true;
} else {
return false;
}
}
public int longestLine(int[][] M) {
int maxVal = 0;
for (int i=0; i<M.length;i++) {
for (int j= 0; j<M[i].length;j++) {
for (int k= 0; k<4; k++)
if (M[i][j] == 1 && (isBoundary(k, i, j, M[i].length - 1) || M[i - dirs[k][0]][j - dirs[k][1]] == 0)) {
int tmpVal = 0;
int ii = i, jj = j;
while (ii >= 0 && ii < M.length && jj >=0 && jj < M[i].length && M[ii][jj] == 1) {
ii += dirs[k][0];
jj += dirs[k][1];
tmpVal++;
}
maxVal = tmpVal > maxVal ? tmpVal : maxVal;
}
}
}
return maxVal;
}
}
```