// C# program to find minimum operations required
// to set all the elements of a binary matrix to 1
using System;
class GfG {
// Return minimum operations required to
// make all elements 1
public static int minOperation(int[,] mat) {
int n = mat.GetLength(0);
int m = mat.GetLength(1);
int ans = 0;
// Start from bottom-right and move to top-left
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
// Check if current cell is 0
if (mat[i,j] == 0) {
// Increment operation count
ans++;
// Flip all elements from (0,0) to (i,j)
for (int k = 0; k <= i; k++) {
for (int h = 0; h <= j; h++) {
// Toggle the element
mat[k,h] = 1 - mat[k,h];
}
}
}
}
}
return ans;
}
// Driver code
public static void Main() {
int[,] mat = {
{0, 0, 1, 1, 1},
{0, 0, 0, 1, 1},
{0, 0, 0, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}
};
Console.WriteLine(minOperation(mat));
}
}