#include using namespace std; int m = 0, n = 0; // lignes, colonnes set regions; void callDFS(vector> &grid, const int i, const int j, const int x) { if(i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) return; if(grid[i][j] == x) { grid[i][j] = 0; callDFS(grid, i + 1, j, x); // Down callDFS(grid, i - 1, j, x); // Up callDFS(grid, i, j + 1, x); // Right callDFS(grid, i, j - 1, x); // Left } } // callDFS() // nombre de régions int numRegions(vector> &grid) { int count = 0; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) { int x = grid[i][j]; if(x != 0) { if(!regions.contains(x)) count++; regions.insert(x); callDFS(grid, i, j, x); } } return count; } // numRegions() int main() { // lecture des données cin >> m >> n; auto g = vector>(m, vector(n, 0)); for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) cin >> g[i][j]; // nombre de régions cout << numRegions(g) << '\n'; return 0; } // main()