/*
给一个数字矩阵,如果上下左右中数值相同,则表示一个连通区域,求矩阵中联通区域的数量
输入:行列数,矩阵 输出:连通域的数量
<span style="white-space:pre"> </span>思路:用BFS遍历
*/
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn = 100;
int n,m;
int matrix[maxn][maxn];
bool inq[maxn][maxn] = {false};
int X[4] = {0,0,1,-1};
int Y[4] = {1,-1,0,0};
struct node{
int x,y;
}Node;
bool judge(int x,int y,int numb){
if(x >=n || x < 0|| y>=m || y< 0) return false; //越界
else if(inq[x][y] == true) return false;
else if(matrix[x][y] == numb) return true;
return false;
}
void BFS(int x,int y,int numb){ //num为当前要比对的数字
queue<node> Q;
int newX,newY;
Node.x = x,Node.y = y;
Q.push(Node);
inq[x][y] = true;
while(!Q.empty()){
node top = Q.front(); // 取队首
Q.pop();
for(int i = 0; i< 4;i++){ //循环取上下左右的连通点 入队列
newX = top.x + X[i];
newY = top.y + Y[i];
if(judge(newX,newY,numb)){
Node.x = newX,Node.y = newY;
Q.push(Node);
inq[newX][newY] = true;
}
}
}
}
int main(){
int ans = 0; //记录连通区域个数
scanf("%d%d",&n,&m);
for(int x = 0; x < n;x++){
for(int y = 0;y < m; y++){
scanf("%d",&matrix[x][y]);
}
}
for(int x = 0;x < n;x++){
for(int y = 0;y < m;y++){
if(inq[x][y] == false){
ans++;
BFS(x,y,matrix[x][y]);
}
}
}
printf("%d\n",ans);
}