#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

struct Field {
	int value;
	char status;
};

Field a[2000][2000];

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			scanf("%d", &a[i][j].value);
			a[i][j].status = ' ';
		}
	}
	int brx=0, bry=0, br=0;
	vector<int> pos;
	vector<int> pon;
	vector<int>::iterator it;
	bool nas = true;
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			if (a[i][j].status == ' ') {
				
				brx = 0;
				bry = 0;
				for (int k=j+1; k<n; k++) {
					if (a[i][j].value == a[i][k].value) brx += 1;
					else break;
				}
				for (int k=i+1; k<n; k++) {
					if (a[i][j].value == a[k][j].value) bry += 1;
					else break;
				}
				
				//printf("%d %d %d %d\n", i, j, brx, bry);
				if (brx==0 && bry==0) {
					it = find (pos.begin(), pos.end(), a[i][j].value);
					
					if (it != pos.end()) {
						it = find (pon.begin(), pon.end(), a[i][j].value);
						if (it != pon.end()) {
							continue;
						} else {
							pon.push_back(a[i][j].value);
							br -= 1;
						}
					}
					
					else {
						pos.push_back(a[i][j].value);
						br += 1;
					}
					
					a[i][j].status = '*';
				}
				
				else {
					nas = true;
					for (int k=i; k<=i+bry; k++) {
						for (int m=j; m<=j+brx; m++) {
							if(a[i][j].value != a[k][m].value) {
								nas = false;
								break;
							}
						}
						if (nas == false) break;
					}
					if (nas == true) {
						for (int k=i; k<=i+bry; k++) {
							for (int m=j; m<=j+brx; m++) {
								a[k][m].status = '*';
							}
						}
						
						it = find (pos.begin(), pos.end(), a[i][j].value);
						
						if (it != pos.end()) {
							it = find (pon.begin(), pon.end(), a[i][j].value);
							if (it != pon.end()) {
								continue;
							} 
							
							else {
								pon.push_back(a[i][j].value);
								br -= 1;
							}
						} else {
							pos.push_back(a[i][j].value);
							br += 1;
						}
					}
				}
			}
		}
	}
	
	printf("%d", br);
}