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

namespace CSharpSample01
{
    
    struct Point
    {
        public int h;
        public int w;
    }


    class Program
    {
        
        
        

        static void DFS(int H, int W, int[,] map, bool[,] visit, int sh, int sw)
        {
            Stack<Point> st = new Stack<Point>();
            st.Push(new Point() { h = sh, w = sw });
            visit[sh, sw] = true;
            bool flag = false;
            Point now;

            while(st.Count != 0)
            {
                flag = false;
                now = st.Peek();
                int nh = now.h;
                int nw = now.w;

                //上
                if(nh - 1 >= 0 && map[nh - 1, nw] == 1 && !visit[nh - 1, nw])
                {
                    visit[nh - 1, nw] = true;
                    st.Push(new Point() { h = nh - 1, w = nw });
                    flag = true;
                }
                //右上
                else if(nh - 1 >= 0 && nw + 1 < W && map[nh - 1, nw + 1] == 1 && !visit[nh - 1, nw + 1])
                {
                    visit[nh - 1, nw + 1] = true;
                    st.Push(new Point() { h = nh - 1, w = nw + 1 });
                    flag = true;
                }              
                //右
                else if(nw + 1 < W && map[nh, nw + 1] == 1 && !visit[nh, nw + 1])
                {
                    visit[nh, nw + 1] = true;
                    st.Push(new Point() { h = nh, w = nw + 1 });
                    flag = true;
                }
                //右下
                else if (nh + 1 < H && nw + 1 < W && map[nh + 1, nw + 1] == 1 && !visit[nh + 1, nw + 1])
                {
                    visit[nh + 1, nw + 1] = true;
                    st.Push(new Point() { h = nh + 1, w = nw + 1 });
                    flag = true;
                }
                //下
                else if (nh + 1 < H && map[nh + 1, nw] == 1 && !visit[nh + 1, nw])
                {
                    visit[nh + 1, nw] = true;
                    st.Push(new Point() { h = nh + 1, w = nw });
                    flag = true;
                }
                //左下
                else if (nh + 1 < H && nw - 1 >= 0 && map[nh + 1, nw - 1] == 1 && !visit[nh + 1, nw - 1])
                {
                    visit[nh + 1, nw - 1] = true;
                    st.Push(new Point() { h = nh + 1, w = nw - 1 });
                    flag = true;
                }
                //左
                else if (nw - 1 >= 0 && map[nh, nw - 1] == 1 && !visit[nh, nw - 1])
                {
                    visit[nh, nw - 1] = true;
                    st.Push(new Point() { h = nh, w = nw - 1 });
                    flag = true;
                }
                //左上
                else if (nh - 1 >= 0 && nw - 1 >= 0 && map[nh - 1, nw - 1] == 1 && !visit[nh - 1, nw - 1])
                {
                    visit[nh - 1, nw - 1] = true;
                    st.Push(new Point() { h = nh - 1, w = nw - 1 });
                    flag = true;
                }


                if (!flag)
                {
                    st.Pop();
                }

            }

        }
        
        static int Solve(int H, int W, int[,] map)
        {
            bool[,] visit = new bool[H, W];
            int cnt_island = 0;

            //visit初期化
            for(int i = 0; i < H; i++)
            {
                for(int j = 0; j < W; j++)
                {
                    visit[i, j] = false;
                }
            }

            for(int i = 0; i < H; i++)
            {
                for(int j = 0; j < W; j++)
                {
                    if(map[i, j] == 1 && !visit[i, j])
                    {
                        DFS(H, W, map, visit, i, j);
                        cnt_island++;
                    }
                }
            }

            

            return cnt_island;
        }

        

        static void Main(string[] args)
        {
            while (true)
            {                
                string[] input = Console.ReadLine().Split(' ');
                int W = int.Parse(input[0]);
                int H = int.Parse(input[1]);

                if (W == 0 && H == 0) break;

                int[,] map = new int[H, W];

                for(int i = 0; i < H; i++)
                {
                    string[] str = Console.ReadLine().Split(' ');
                    for(int j = 0; j < W; j++)
                    {
                        if (str[j] == "1") map[i, j] = 1;
                        else map[i, j] = 0;
                    }
                }
              

                Console.WriteLine(Solve(H, W, map));
            }
            
        }
    }
}


Measured memory_bytes: 18276