onesRow and onesCol with zeroes, with sizes equal to the number of rows M and columns N of the input matrix grid, respectively.(i, j) in the matrix grid and increment onesRow[i] and onesCol[j] by grid[i][j].diff with the same dimensions as grid.(i, j) in the matrix grid again and calculate diff[i][j] using the formula 2 * onesRow[i] + 2 * onesCol[j] - N - M.diff.