function checkBoard(b,n){
function chunk2D(b,n){
var chunks = [],
chunk;
if (!n || b.length < n || b[0].length < n ) return "no way..!";
for (var i = 0; i <= b.length - n; i++){
for (var j = 0; j <= b[0].length - n; j++){
chunk = {x:j, y:i, c:[]};
for (var k = 0; k < n; k++){
chunk.c.push(b[i+k].slice(j,j+n));
}
chunks.push(chunk);
chunk = [];
}
}
return chunks;
}
function getDiagonals(a){
var len = a.length,
result = [[],[]];
for (var i = 0; i < len; i++){
result[0].push(a[i][i]);
result[1].push(a[i][len-1-i]);
}
return result;
}
function getColumns(a){
return a.map((r,i) => r.map((_,j) => a[j][i]));
}
var chunks = chunk2D(b,n),
diags,
columns,
found;
return chunks.reduce(function(r,c,i){
diags = getDiagonals(c.c);
found = diags[0].reduce((p,d) => p !== 0 && d !== 0 && p == d ? d : 0);
found && (r["sx: " + c.x + ", sy: " + c.y + ", ex: " + (c.x+n-1) + ", ey: " + (c.y+n-1)] = [[c.x,c.y],[c.x+n-1,c.y+n-1],found]);
found = diags[1].reduce((p,d) => p !== 0 && d !== 0 && p == d ? d : 0);
found && (r["sx: " + c.x + ", sy: " + (c.y+n-1) + ", ex: " + (c.x+n-1) + ", ey: " + c.y] = [[c.x,c.y+n-1],[c.x+n-1,c.y],found]);
columns = getColumns(c.c);
columns.forEach(function(col,j){
found = col.reduce((p,d) => p !== 0 && d !== 0 && p == d ? d : 0);
found && (r["sx: " + (c.x+j) + ", sy: " + c.y + ", ex: " + (c.x+j) + ", ey: " + (c.y+n-1)] = [[c.x+j,c.y],[c.x+j,c.y+n-1],found]);
});
c.c.forEach(function(row,j){
found = row.reduce((p,d) => p !== 0 && d !== 0 && p == d ? d : 0);
found && (r["sx: " + c.x + ", sy: " + (c.y+j) + ", ex: " + (c.x+n-1) + ", ey: " + (c.y+j)] = [[c.x,c.y+j],[c.x+n-1,c.y+j],found]);
});
return r;
}, {});
}
var board = [[0, 0, 2, 0, 0],
[0, 2, 2, 0, 0],
[0, 1, 2, 0, 0],
[0, 1, 2, 2, 0],
[1, 1, 1, 1, 2]];
console.log(checkBoard(board,4));