from collections import defaultdict h,w = map(int,input().split()) A = [list(map(int,input().split())) for i in range(h)] xy = [[] for i in range(5*10**5+5)] for i in range(h): for j in range(w): xy[A[i][j]].append((i,j)) ans = 0 for i in range(5*10**5,0,-1): if xy[i] == []: continue dich = defaultdict(int) dicw = defaultdict(int) for x,y in xy[i]: dich[x] += 1 dicw[y] += 1 dels = set() while True: mh = 0 ih = 0 mw = 0 iw = 0 for k,v in dich.items(): if v > mh: mh = v ih = k for k,v in dicw.items(): if v > mw: mw = v iw = k if mh == mw == 0: break ans += 1 if mh >= mw: dich[ih] = 0 for x,y in xy[i]: if (x,y) in dels: continue if x == ih: dicw[y] -= 1 dels.add((x,y)) else: dicw[iw] = 0 for x,y in xy[i]: if (x,y) in dels: continue if y == iw: dich[x] -= 1 dels.add((x,y)) print(ans)