H, W = map(int, input().split()) S = [] for i in range(H): for j, a in enumerate(list(map(int, input().split()))): if a > 0: S.append((a, i, j)) S.sort(reverse=True) cnt = 0 while len(S) > 0: A = [] A.append(S.pop(0)) while len(S) > 0 and A[0][0] == S[0][0]: A.append(S.pop(0)) Q = [(A, 1)] while len(Q) > 0: B, n = Q.pop(0) I = [] for b in B[1:]: if B[0][1] != b[1]: I.append(b) if len(I) == 0: cnt += n break Q.append((I, n+1)) J = [] for b in B[1:]: if B[0][2] != b[2]: J.append(b) if len(J) == 0: cnt += n break Q.append((J, n+1)) print(cnt)