# Python implementation
def find(m):
stack = []
c = -1
for i in range(len(m)):
stack.append(i)
while len(stack) > 1:
i = stack.pop()
j = stack.pop()
if m[i][j] == 1:
stack.append(j)
else:
stack.append(i)
c = stack.pop()
for i in range(len(m)):
if i != c and (m[i][c] == 0 or m[c][i] == 1):
return -1
return c
matrix = [
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]
]
print(find(matrix))
// Javascript implementation
function find(m) {
const stack = [];
let c = -1;
for (let i = 0; i < m.length; i++) {
stack.push(i);
}
while (stack.length > 1) {
const i = stack.pop();
const j = stack.pop();
if (m[i][j] == 1) {
stack.push(j);
} else {
stack.push(i);
}
}
c = stack.pop()
for (let i = 0; i < m.length; i++) {
if (i != c && (m[i][c] == 0 || m[c][i] == 1)) {
return -1;
}
}
return c;
}
const matrix = [
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]
];
console.log(find(matrix));