Sobes.tech
Middle+

How many connected regions are in the matrix (Number of Connected Regions)? Condition Given a 2D matrix of integers. Each cell contains a number. Two cells are considered connected if they are adjacent horizontally or vertically and contain the same number. A connected region is a maximum group of cells with the same value, connected to each other. Write a function that returns the number of such connected regions in the matrix. Input data A 2D array of integers grid (matrix size m×n). Output data An integer — the number of connected regions. Constraints 1 <= m, n <= 100 Code template: # Available without import: json, sys, io def count_regions(grid): # TODO: implement the solution here return 0