<p>Given a <code>m x n</code> binary matrix <code>mat</code>. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing <code>1</code> to <code>0</code> and <code>0</code> to <code>1</code>). A pair of cells are called neighbors if they share one edge.</p>

<p>Return the <em>minimum number of steps</em> required to convert <code>mat</code> to a zero matrix or <code>-1</code> if you cannot.</p>

<p>A <strong>binary matrix</strong> is a matrix with all cells equal to <code>0</code> or <code>1</code> only.</p>

<p>A <strong>zero matrix</strong> is a matrix with all cells equal to <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" style="width: 409px; height: 86px;" />
<pre>
<strong>Input:</strong> mat = [[0,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Given matrix is a zero matrix. We do not need to change it.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,0,0],[1,0,0]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Given matrix cannot be a zero matrix.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 3</code></li>
	<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>



#include <bits/stdc++.h>
using namespace std;
#define read(type) readInt<type>() // Fast read
// #define int long long
#define nL "\n"

#define vi vector<int>
#define aint(x) (x).begin(), (x).end()
#define umap unordered_map
#define uset unordered_set
#define MOD 1000000007
#define imax INT_MAX
#define imin INT_MIN
#define exp 1e9
#define sz(x) (int((x).size()))
#define pq_max priority_queue<int>
#define pq_min priority_queue<int, vi, greater<int>>
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

/* FUNCTIONS */
#define f(i, s, e) for (int i = s; i <= e; i++)
#define rf(i, e, s) for (int i = e; i >= s; i--)



class Solution
{
public:
    bool allZEr(vector<vector<int>> &mat)
    {
        for (int i = 0; i < mat.size(); i++)
        {
            for (int j = 0; j < mat[0].size(); j++)
            {
                if (mat[i][j] == 1)
                    return false;
            }
        }
        return true;
    }

    void flip_aelf_nbr(int i, int j, vector<vector<int>> &mat)
    {
        int n = mat.size();
        int m = mat[0].size();
        int row_nbr[] = {0, 0, 0, -1, 1};
        int col_nbr[] = {0, -1, 1, 0, 0};

        for (int k = 0; k < 5; k++)
        {
            int x = i + row_nbr[k];
            int y = j + col_nbr[k];

            if (x >= 0 && x < n && y >= 0 && y < m)
            {
                mat[x][y] = 1 ^ mat[x][y];
            }
        }
    }

    int rec_sol(int i, int j, vector<vector<int>> &mat)
    {
        // 2 chocie flip or not
        int n = mat.size();
        int m = mat[0].size();

        // cout << "I: " << i << " J: " << j << nL;

        if (allZEr(mat))
            return 0;

        if (i == n)
        {
            return 1e5;
        }

        // flip

        int flip = 0, no_flip = 0;

        flip_aelf_nbr(i, j, mat);
        if (j < m - 1)
        {
            flip = 1 + rec_sol(i, j + 1, mat);
        }
        else
        {
            flip = 1 + rec_sol(i + 1, 0, mat);
        }
        flip_aelf_nbr(i, j, mat); // to revert back

        // no flip

        if (j < m - 1)
        {
            no_flip = rec_sol(i, j + 1, mat);
        }
        else
        {
            no_flip = rec_sol(i + 1, 0, mat);
        }

        return min(flip, no_flip);
    }

    int minFlips(vector<vector<int>> &mat)
    {

        int ans= rec_sol(0, 0, mat);

        return (ans==1e5?-1:ans);
    }
};
7900