Python Simple Solution with explanation
For a 3x3 array, a matrix of its indices would look something like, \n\n\nFor a 4x4 array, a matrix of its indices would look something like, \n\n\nAs you can s
For a 3x3 array, a matrix of its indices would look something like, \n![image](https://assets.leetcode.com/users/images/e18b8253-6f47-40fc-9ee9-d126807f54f7_1613697865.805011.png)\n\nFor a 4x4 array, a matrix of its indices would look something like, \n![image](https://assets.leetcode.com/users/images/87e0b0db-e4df-4038-914b-df94d47da34b_1613697934.5044467.png)\n\nAs you can see there\'s a clear pattern that the indices of the diagonal elements follow. \n1. For the diagonal starting top-left ending bottom-right, index i = index j\n2. For the diagonal starting bottom-left ending top-right, index i + index j = number of rows/columns - 1\n\nThis is what we need to implement in the code.\n\n```\nclass Solution:\n    def diagonalSum(self, mat: List[List[int]]) -> int:\n        nrows = len(mat)\n        ncols = len(mat[0])\n        output = 0\n        for i in range(nrows):\n            for j in range(ncols):\n                if i==j or i+j == nrows-1:\n                    output+=mat[i][j]\n        return output\n
['Python']