
If you need to only loop through the diagonal elements of the two-dimensional array you can use the following C# code (but should be more or less the same for any programming language). It calculates the diagonal index based on a simple formula and has O(n) complexity, where n = max(width, height)