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; public static int[] dx, dy; static void Main() { dx = new int[] { 0, 1, 0, -1 }; dy = new int[] { 1, 0, -1, 0 }; 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, int sx, int sy) { var q = new Queue
(); q.Enqueue(new P { x = sx, y = sy }); map[sx, sy] = false; 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]) { q.Enqueue(new P { x = nx, y = ny }); map[nx, ny] = false; } } } } static void getAns(bool[,] map) { var count = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (map[i, j]) { count++; Bfs(map, i, j); } } Console.WriteLine(count); } }