using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); int rowCount = inpt[0]; int colCount = inpt[1]; bool[,] map = new bool[rowCount, colCount]; for (int i = 0; i < rowCount; i++) { inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); for (int j = 0; j < colCount; j++) { if(inpt[j] == 1) { map[i, j] = true; } } } int[,] group = new int[rowCount, colCount]; int ans = 0; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { if(!map[i,j]) { continue; } if(group[i,j] != 0) { continue; } ans++; group[i, j] = ans; Queue q = new Queue(); q.Enqueue(new int[]{i,j}); while(q.Count > 0) { int[] pos = q.Dequeue(); for (int k = Math.Max(i - 1, 0); k <= Math.Min(i + 1, rowCount - 1); k++) { for (int l = Math.Max(j - 1, 0); l <= Math.Min(j + 1, colCount - 1); l++) { if(i==k&&j==l) { continue; } if(i!=k && j!=l) { continue; } if(!map[k,l]) { continue; } if(group[k,l] != 0) { continue; } group[k, l] = group[i, j]; q.Enqueue(new int[] { k, l }); } } } } } Console.WriteLine(ans); } public class Reader { static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 4 3 1 0 1 0 1 0 1 0 1 0 1 0 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }