#include <bits/stdc++.h>

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")
using namespace std;

typedef int ll;
typedef long double ld;

#define ff first
#define ss second
#define pb push_back
#define el "\n"
#define se " "

const ll N=400+9;
const ll INF=1e18;

ll n,m,a,b,c,d,k,kk,i,j,cnt,cnt1,res,res1,sum,sum1,val,cur;
ll AA[N][N];
pair<ll,ll>pp[N*N];
unordered_map<ll,bool>ump;
int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            cin>>AA[i][j];
        }
    }
    ll cut[n+5][m+5]={0};
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            cut[i][AA[i][j]]++;
        }
        for(j=1;j<=m;j++){
            if(AA[i][j]==0){
                ll ans=0;
                for(ll it=i+1;it<=n;it++){
                    if(ans==1) break;
                    for(ll jt=1;jt<=m;jt++){
                        cut[it][jt]=0;
                    }
                    for(ll jt=1;jt<=m;jt++){
                        cut[it][AA[it][jt]]++;
                    }
                    if(cut[it][AA[i][j]]>1){
                       for(ll jt=1;jt<=m;jt++){
                            if(ans==1) break;
                            if(AA[it][jt]==AA[i][j]){
                                AA[it][jt]=0;
                                pp[a].ff=it*m+jt,pp[a].ss=n*m+1,a++;
                                pp[a].ff=n*m+1,pp[a].ss=i*m+j,a++;
                                ans=1;
                                break;
                            }
                       }
                    }
                }
            }
        }
    }
    cout<<min(res,a);
}
