using System.Linq; using System.Collections.Generic; using System; public class P { public int x { get; set; } public int y { get; set; } } public class Hello { public static int h, w; static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); h = int.Parse(line[0]); w = int.Parse(line[1]); var map = new bool[h, w]; for (int i = 0; i < h; i++) { line = Console.ReadLine().Trim().Split(' '); for (int j = 0; j < w; j++) if (line[j] == "1") map[i, j] = true; } getAns(map); } static void Bfs(bool[,] map, bool[,] visited, int sx, int sy) { var dx = new int[] { 0, 1, 0, -1 }; var dy = new int[] { 1, 0, -1, 0 }; var q = new Queue

(); q.Enqueue(new P { x = sx, y = sy }); visited[sx, sy] = true; while (q.Count() > 0) { var t = q.Dequeue(); for (int i = 0; i < 4; i++) { var nx = t.x + dx[i]; var ny = t.y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx, ny] && !visited[nx, ny]) { q.Enqueue(new P { x = nx, y = ny }); visited[nx, ny] = true; } } } } static void getAns(bool[,] map) { var visited = new bool[h, w]; var count = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (map[i, j] && !visited[i, j]) { count++; Bfs(map, visited, i, j); } } Console.WriteLine(count); } }