import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def SI(): return sys.stdin.readline()[:-1] def MI(): return map(int, sys.stdin.readline().split()) def MI1(): return map(int1, sys.stdin.readline().split()) def MF(): return map(float, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LF(): return list(map(float, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] dij = [(0, 1), (1, 0), (0, -1), (-1, 0)] def main(): n,m=MI() mem=[[] for _ in range(m)] val=[-1]*m no1=[[False]*m for _ in range(n)] ans=0 for i in range(n): rr=LI() for j,r in enumerate(rr): if r>val[j]: while mem[j]: ii=mem[j].pop() no1[ii][j]=False if sum(no1[ii])==0:ans-=1 val[j]=r mem[j].append(i) no1[i][j]=True if sum(no1[i])==1:ans+=1 elif r==val[j]: mem[j].append(i) no1[i][j]=True if sum(no1[i])==1:ans+=1 print(ans) main()