a square matrix say xmatrix follow condition holdall element diagonal matrix nonzeroall element 0given 2D integer array grid size n x n represent square matrix return true grid XMatrix otherwise return false example 1input grid 2001031005204002output trueexplanation refer diagram an XMatrix green element diagonal nonzero red element 0Thus grid XMatrixExample 2input grid 570031050output falseexplanation Refer diagram aboveAn XMatrix green element diagonal nonzero red element 0Thus grid XMatrix Constraintsn gridlength gridilength3 n 1000 gridij 105