m x n matrix answer with zeros, where m is the number of rows and n is the number of columns in the input grid.(r, c) in the grid.topLeft and bottomRight.(r - 1, c - 1) and moving diagonally upwards. Add each encountered value to the topLeft set.(r + 1, c + 1) and moving diagonally downwards. Add each encountered value to the bottomRight set.topLeft and bottomRight sets and store it in answer[r][c].answer matrix after filling in all the cells.