using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompetitiveProg
{
    class Program
    {
        static int w, h;
        static int[,] map;
        static bool[,] cellVisited;
        static int count;
        static void Main(string[] args)
        {
            var ans = new List<int>();
            while(true)
            {
                count = 0;
                var wh = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
                if (wh[0] == 0 && wh[1] == 0) break;
                w = wh[0]; h = wh[1];
                map = new int[w, h];
                cellVisited = new bool[w, h];
             
                for (int j = 0; j < h; j++)
                {
                    var row = Console.ReadLine().Split(' ');
                    var i = 0;
                    foreach (var r in row)
                    {
                        switch (r)
                        {
                            case "1":
                                map[i, j] = 1;
                                break;
                            case "0":
                                map[i, j] = 0;
                                break;
                         }
                        i++;
                    }


                }
                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < h; j++)
                    {
                        cellVisited[i, j] = false;
                    }
                }
                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < h; j++)
                    {
                        if (!cellVisited[i, j])
                        {
                            dfs(i, j);
                            if (map[i, j] == 1) count++;
                        }
                    }

                }
                ans.Add(count);
            }
            foreach (var c in ans)
            {
                Console.WriteLine(c);
            }
            

        }
        static void dfs(int x,int y)
        {
            
            if (cellVisited[x, y]) return;
            cellVisited[x, y] = true;
            if (map[x, y] == 0) return;
            if ((x - 1 >= 0) && map[x - 1, y] == 1)
            {
                dfs(x - 1, y);
            }
            if ((y - 1 >= 0) && map[x, y - 1] == 1)
            {

                
                dfs(x, y - 1);
            }
            if ((x + 1 < w) && map[x + 1, y] == 1)
            {
                dfs(x + 1, y);
            }
            if ((y + 1 < h) && map[x, y + 1] == 1)
            {

                dfs(x, y + 1);
            }
            if ((x - 1 >= 0)&&(y-1>=0) && map[x - 1, y-1] == 1)
            {
                
                dfs(x - 1, y-1);
            }
            if ((y + 1 < h)&&(x + 1 < w) && map[x+1, y + 1] == 1)
            {
              
                dfs(x+1, y + 1);
            }
            if ((x + 1 < w) && (y - 1 >= 0) && map[x + 1, y-1] == 1)
            {
                dfs(x + 1, y-1);
            }
            if ((y + 1 < h) && (x - 1 >= 0) && map[x-1, y + 1] == 1)
            {
                
                dfs(x-1, y + 1);
            }

            
         }
       
    }
}

Measured memory_bytes: 6340