using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProgrammingContest { class Writer : IDisposable { private readonly System.IO.TextWriter writer; private StringBuilder sb; private readonly bool isReactive; public Writer(string path) : this(new System.IO.StreamWriter(path)) { } public Writer(bool isReactive) : this(null, isReactive) { } public Writer(System.IO.TextWriter writer = null, bool isReactive = false) { this.writer = (writer ?? Console.Out); this.isReactive = isReactive; if (!this.isReactive) { this.sb = new StringBuilder(); } } public void Dispose() { if (!this.isReactive) { this.writer.Write(sb.ToString()); } if (!this.writer.Equals(Console.Out)) { this.writer.Dispose(); } } public void Write(object val) { if (this.isReactive) { this.writer.Write(val.ToString()); this.writer.Flush(); } else { this.sb.Append(val.ToString()); } } public void Write(string format, params object[] vals) { if (this.isReactive) { this.writer.Write(format, vals); this.writer.Flush(); } else { this.sb.AppendFormat(format, vals); } } public void WriteLine(object val = null) { this.Write((val ?? string.Empty).ToString() + System.Environment.NewLine); } public void WriteLine(int val) { this.WriteLine(val.ToString()); } public void WriteLine(long val) { this.WriteLine(val.ToString()); } public void WriteLine(string val) { this.WriteLine((object)val); } public void WriteLine(string format, params object[] vals) { this.Write(format + System.Environment.NewLine, vals); } } class Scanner : IDisposable { private readonly Queue buffer; private readonly char[] sep; private readonly System.IO.TextReader reader; public Scanner(string path, char[] sep = null) : this(new System.IO.StreamReader(path), sep) { } public Scanner(System.IO.TextReader reader = null, char[] sep = null) { this.buffer = new Queue(); this.sep = (sep ?? new char[] { ' ' }); this.reader = (reader ?? Console.In); } private void CheckBuffer() { if (this.buffer.Count == 0 && this.reader.Peek() != -1) { string str = string.Empty; for (; string.IsNullOrEmpty(str); str = this.reader.ReadLine()) ; str.Split(this.sep).ToList() .ForEach(el => this.buffer.Enqueue(el)); } } public void Dispose() { if (!this.reader.Equals(Console.In)) { this.reader.Dispose(); } } public string Next { get { this.CheckBuffer(); return this.buffer.Dequeue(); } } public string[] GetStringArray(int N) { return Enumerable.Range(0, N) .Select(e => this.Next) .ToArray(); } public int NextInt { get { return int.Parse(this.Next); } } public int[] GetIntArray(int N) { return Enumerable.Range(0, N) .Select(e => this.NextInt) .ToArray(); } public double NextDouble { get { return double.Parse(this.Next); } } public double[] GetdoubleArray(int N) { return Enumerable.Range(0, N) .Select(e => this.NextDouble) .ToArray(); } public long NextLong { get { return long.Parse(this.Next); } } public long[] GetLongArray(int N) { return Enumerable.Range(0, N) .Select(e => this.NextLong) .ToArray(); } public bool IsEnd { get { this.CheckBuffer(); return this.buffer.Count == 0; } } } class MainClass : IDisposable { Scanner sc; Writer wr; const string backPath = ".."; private readonly char dirSep = System.IO.Path.DirectorySeparatorChar; private readonly string inFilePath = string.Empty; private readonly string outFilePath = string.Empty; public MainClass() { this.inFilePath = MainClass.backPath + this.dirSep + MainClass.backPath + this.dirSep + "in.txt"; this.outFilePath = MainClass.backPath + this.dirSep + MainClass.backPath + this.dirSep + "out.txt"; this.wr = new Writer(this.IsReactive); //this.wr = new Writer(this.outFilePath); #if DEBUG if (!this.IsReactive) { this.sc = new Scanner(this.inFilePath); } else { this.sc = new Scanner(); } #else this.sc = new Scanner(); #endif } static void Main(string[] args) { using (var mainClass = new MainClass()) { mainClass.Solve(); } } public void Dispose() { if (this.sc != null) { this.sc.Dispose(); this.sc = null; } if (this.wr != null) { this.wr.Dispose(); this.wr = null; } #if DEBUG Console.WriteLine("続行するには何かキーを押してください..."); Console.ReadKey(); #endif } void MakeTestCase() { var rand = new Random(); if (this.wr != null) { this.wr.Dispose(); } this.wr = new Writer(inFilePath); } void Solve() { int H = sc.NextInt; int W = sc.NextInt; bool[,] A = new bool[H, W]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { A[i, j] = sc.NextInt == 1; } } int[] nh = new int[] { -1, 0, 0, 1 }; int[] nw = new int[] { 0, -1, 1, 0 }; bool[,] vis = new bool[H, W]; Queue> que = new Queue>(); Func In = (ii, jj) => { return 0 <= ii && ii < H && 0 <= jj && jj < W; }; int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (A[i, j] && !vis[i, j]) { ans++; que.Enqueue(new Pair() { First = i, Second = j }); while (que.Count > 0) { var p = que.Dequeue(); if (!In(p.First, p.Second) || vis[p.First, p.Second] || !A[p.First, p.Second]) { continue; } vis[p.First, p.Second] = true; for (int k = 0; k < 4; k++) { int ni = p.First + nh[k]; int nj = p.Second + nw[k]; que.Enqueue(new Pair() { First = ni, Second = nj }); } } } } } wr.WriteLine(ans); } class Pair : IComparable> { public T1 First { get; set; } public T2 Second { get; set; } public int CompareTo(Pair other) { int ret = 0; if (First is IComparable) { ret = (First as IComparable).CompareTo(other.First); } if (ret == 0 && Second is IComparable) { ret = (Second as IComparable).CompareTo(other.Second); } return ret; } } private bool IsReactive { get; } = false; // TODO: reactive check !! } }