A square matrix is said to be an X-Matrix if both of the following conditions hold:


	All the elements in the diagonals of the matrix are non-zero.
	All other elements are 0.


Given a 2D integer array grid of size n x n representing a square matrix, return true if grid is an X-Matrix. Otherwise, return false.

&nbsp;
Example 1:

Input: grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
Output: true
Explanation: Refer to the diagram above. 
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is an X-Matrix.


Example 2:

Input: grid = [[5,7,0],[0,3,1],[0,5,0]]
Output: false
Explanation: Refer to the diagram above.
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is not an X-Matrix.


&nbsp;
Constraints:


	n == grid.length == grid[i].length
	3 &lt;= n &lt;= 100
	0 &lt;= grid[i][j] &lt;= 105


class Solution:
    def checkXMatrix(self, grid: List[List[int]]) -> bool:
        n = len(grid)
        for i in range(n):
            for j in range(n):
                if i==j or (i+j) ==n-1:
                    if grid[i][j] == 0:
                        return False
                elif grid[i][j] != 0: 
                    return False
        return True;
class Solution {
    public boolean checkXMatrix(int[][] grid) {
        
        int n = grid.length;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++ ){
                if ( i == j  || i + j == n - 1 ) {
                    if ( grid[i][j] == 0 ) return false;                    
                }
				else if ( grid[i][j] != 0 ) return false;                                    
            }
        }       
       return true;
    }
}
class Solution {
public:
    bool checkXMatrix(vector<vector<int>>& grid) {
        int n = grid.size();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(i == j or i == n - j - 1){
                    if(!grid[i][j])
                        return false;
                }else if(grid[i][j])
                    return false;    
            }
        }
        
        return true;
    }
};
var checkXMatrix = function(grid) {
    for (let i=0; i<grid.length; i++) {
        for (let j=0; j<grid[i].length; j++) {
            let leftDiagonal = grid[i].length - 1;
            if (i === j && grid[i][j] !== 0 || j === leftDiagonal - i && grid[i][j] !== 0) {
                continue;
            }
            if (i === j && grid[i][j] === 0 || j === leftDiagonal - i && grid[i][j] === 0) {
                return false;
            }
            if (grid[i][j] !== 0) {
                return false;
            }
        }
    }
    return true;
};
Check if Matrix Is X-Matrix