For this Question: You are given a two dimensional matrix with N rows and M columns. Each cell (i, j) of the matrix has some value denoted by A<sub>i,j</sub>.

You can do some operations on the matrix (possibly many or zero). In a single operation, you can choose any two adjacent cells and increment the value of each of them by 1. Find the minimum number of operations to make the values of all the cells of the matrix equal. If it's not possible to make all cells equal print -1.The first line contains two space-separated integers N and M — denoting the size of the matrix.
Each of the next N lines contains M space-separated integers representing the values of the matrix, where the j<sup>th</sup> integer on i<sup>th</sup> line denotes the value of cell (i, j) — A<sub>i,j</sub>.

<b>Constraints:</b>
1 ≤ N, M ≤ 50
-10<sup>9</sup> ≤ A<sub>i,j</sub> ≤ 10<sup>9</sup>Print a single integer — the minimum number of operations required to make all the cells equal. If it is not possible, print -1.Sample Input 1:
2 2
1 0 
0 -1 

Sample Output 1:
2

Explanation:
One optimal sequence of moves is to first do the operation on cells (1, 2) and (2, 2), and then on cells (2, 1) and (2, 2).
After this, all cells will have a value of 1.

Sample Input 2:
3 3
-2 0 -3 
1 2 1 
1 -3 1 

Sample Output 2:
-1, I have written this Solution Code: 
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());



long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
    if (b & 1)
        res = res * a %mod;
    a = a * a %mod;
    b >>= 1;
}

return res;
}

ll gcd(ll a, ll b)
{
if (b == 0)
    return a;
return gcd(b, a % b);      
}


struct FlowEdge {
    int v, u;
    long long cap, flow = 0;
    FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};

struct Dinic {
    const long long flow_inf = 1e18;
    vector<FlowEdge> edges;
    vector<vector<int>> adj;
    int n, m = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;

    Dinic(int n, int s, int t) : n(n), s(s), t(t) {
        adj.resize(n);
        level.resize(n);
        ptr.resize(n);
    }

    void add_edge(int v, int u, long long cap) {
        edges.emplace_back(v, u, cap);
        edges.emplace_back(u, v, 0);
        adj[v].push_back(m);
        adj[u].push_back(m + 1);
        m += 2;
    }

    bool bfs() {
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : adj[v]) {
                if (edges[id].cap - edges[id].flow < 1)
                    continue;
                if (level[edges[id].u] != -1)
                    continue;
                level[edges[id].u] = level[v] + 1;
                q.push(edges[id].u);
            }
        }
        return level[t] != -1;
    }

    long long dfs(int v, long long pushed) {
        if (pushed == 0)
            return 0;
        if (v == t)
            return pushed;
        for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
            int id = adj[v][cid];
            int u = edges[id].u;
            if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
                continue;
            long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
            if (tr == 0)
                continue;
            edges[id].flow += tr;
            edges[id ^ 1].flow -= tr;
            return tr;
        }
        return 0;
    }

    long long flow() {
        long long f = 0;
        while (true) {
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            q.push(s);
            if (!bfs())
                break;
            fill(ptr.begin(), ptr.end(), 0);
            while (long long pushed = dfs(s, flow_inf)) {
                f += pushed;
            }
        }
        return f;
    }
};




int a[205][205];
int n,m;

int find_ind(int x,int y)
{
    return (x-1)*m+y;
}

bool check(int val)
{
    int source=n*m+1;
    int sink=n*m+2;
    Dinic kad(n*m+10,source,sink);
    int lim=1e17;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if((i+j)%2)
            {
                if(i+1<=n)
                {
                    kad.add_edge(find_ind(i,j),find_ind(i+1,j),lim);
                    // cout<<find_ind(i,j)<<" "<<find_ind(i+1,j)<<endl;
                }
                if(j+1<=m)
                {
                    kad.add_edge(find_ind(i,j),find_ind(i,j+1),lim);
                    // cout<<find_ind(i,j)<<" "<<find_ind(i,j+1)<<endl;
                }

                if(i-1>=1)
                {
                    kad.add_edge(find_ind(i,j),find_ind(i-1,j),lim);
                    // cout<<find_ind(i,j)<<" "<<find_ind(i-1,j)<<endl;
                }
                if(j-1>=1)
                {
                    kad.add_edge(find_ind(i,j),find_ind(i,j-1),lim);
                    // cout<<find_ind(i,j)<<" "<<find_ind(i,j+1)<<endl;
                }

            }
        }
    }

    int tot=0;
    int req=0;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if((i+j)%2)
            {
                req+=val-a[i][j];
                tot+=val-a[i][j];
                kad.add_edge(source,find_ind(i,j),val-a[i][j]);
                // cout<<source<<" "<<find_ind(i,j)<<" "<<val-a[i][j]<<endl;
            }
            else
            {
                tot-=val-a[i][j];
                kad.add_edge(find_ind(i,j),sink,val-a[i][j]);
                // cout<<find_ind(i,j)<<" "<<sink<<" "<<val-a[i][j]<<endl;
            }
        }
    }


    if(tot!=0)
        return 0;

    int ff=kad.flow();
    // cout<<val<<" "<<ff<<endl;

    if(ff!=req)
        return 0;
    return 1;
}


signed main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);

#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#endif
        
    
    cin>>n>>m;
    int sum1=0,sum2=0;
    int cnt1=0,cnt2=0;
    int mx=-1e17;


    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
            mx=max(mx,a[i][j]);
            if((i+j)%2)
            {
                sum1+=a[i][j];
                cnt1++;
            }
            else
            {
                sum2+=a[i][j];
                cnt2++;
            }
        }



    if(n%2 && m%2)
    {
        int z=sum1-sum2;
        z*=(cnt1-cnt2);
        if(z>=mx && check(z))
            cout<<(z*n*m-(sum1+sum2))/2<<'\n';
        else
            cout<<-1<<'\n';
    }
    else
    {
        int l=mx,r=1e12;
        int fi=1e18;


        while(l<=r)
        {
            int mid=l+(r-l)/2;

            if(check(mid))
            {
                r=mid-1;
                fi=min(fi,(mid*n*m-(sum1+sum2))/2);
            }
            else
            {
                l=mid+1;
            }
        }

        if(fi==1e18)
            cout<<-1<<'\n';
        else
            cout<<fi<<'\n';
    }

}
        , In this Programming Language: C++, Now tell me if this Code is compilable or not?