Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
If <code>grid[x][y]</code> is 1, it can form a right triangle with an element of <code>grid</code> with value 1 in the same row and an element of <code>grid</code> with value 1 in the same column.
So we just need to count the number of 1s in each row and column.
For each <code>x, y</code> with <code>grid[x][y] = 1</code> if there are <code>row[x]</code> 1s in the row <code>x</code> and <code>col[y]</code> 1s in column <code>y</code>, the answer should be added by <code>(row[x] - 1) * (col[y] - 1)</code>.