#pragma warning disable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

static class MainClass
{
    public static void Main()
    {
        int[] dh = { 1, 1, 0, -1, -1, -1, 0, 1 };
        int[] dw = { 0, -1, -1, -1, 0, 1, 1, 1 };
        while (true)
        {
            var lkcount = 0;
            var hw = Console.ReadLine().SplittedStringToInt32List();
            var w = hw[0];
            var h = hw[1];
            if (h + w == 0)
                break;
            bool[,] ls = new bool[h, w];
            for (int i = 0; i < h; i++)
            {
                int count = 0;
                Console.ReadLine().SplittedStringToInt32List().ForEach(x =>
                {
                    var val = x == 1;
                    ls[i, count] = val;
                    count++;
                });
            }
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    if (ls[i, j])
                    {
                        KeyValuePair<int, int> item;
                        var stk = new Stack<KeyValuePair<int, int>>();
                        stk.Push(new KeyValuePair<int, int>(i, j));
                        while (true)
                        {
                            try { item = stk.Pop(); } catch { break; }
                            for (int k = 0; k < 8; k++)
                            {
                                var hhh = item.Key + dh[k];
                                var www = item.Value + dw[k];
                                if (hhh < 0 || www < 0 || hhh >= h || www >= w)
                                    continue;
                                if (ls[hhh, www])
                                {
                                    stk.Push(new KeyValuePair<int, int>(hhh, www));
                                }
                            }
                            ls[item.Key, item.Value] = false;

                        }
                        lkcount++;
                    }
                }
            }

            Console.WriteLine(lkcount);
        }
        Console.ReadLine();
    }

    #region ライブラリ
    #region 二分探索
    public static int LowerBound<T>(this IEnumerable<T> ar, int start, int end, T value, IComparer<T> comparer)
    {
        var arr = ar.ToArray();
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) < 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int LowerBound<T>(this IEnumerable<T> arr, T value) where T : IComparable
    {
        return LowerBound(arr, 0, arr.Count(), value, Comparer<T>.Default);
    }

    public static int UpperBound<T>(this IEnumerable<T> ar, int start, int end, T value, IComparer<T> comparer)
    {
        var arr = ar.ToArray();
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) <= 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int UpperBound<T>(this IEnumerable<T> arr, T value)
    {
        return UpperBound(arr, 0, arr.Count(), value, Comparer<T>.Default);
    }
    #endregion
    #region　bit全探索
    /// <summary>
    /// Bit全探索用のライブラリ
    /// </summary>
    /// <param name="len">作る配列の長さ(lenが0以下だと（ｒｙ)</param>
    /// <returns>できた配列</returns>
    public static List<List<bool>> CreateBits(int len)
    {
        return CreateBitsPri(new List<List<bool>>(), len, 0);
    }

    private static List<List<bool>> CreateBitsPri(List<List<bool>> bits, int len, int count)
    {
        if (count == 0)
        {
            var lss = new List<bool>();
            lss.Add(true);
            bits.Add(lss);
            var lx = new List<bool>();
            lx.Add(false);
            bits.Add(lx);
            count++;
            return CreateBitsPri(bits, len, count);
        }
        if (len - count == 0)
            return bits;
        count++;
        var ls = new List<List<bool>>();
        foreach (var item in bits)
        {
            var x = item.ToList();
            x.Add(true);
            item.Add(false);
            ls.Add(item);
            ls.Add(x);
        }
        return CreateBitsPri(ls, len, count);
    }
    #endregion
    #region 文字列処理

    public static int ToInt32(this string str)
    {
        return int.Parse(str);
    }
    public static List<string> SplittedStringToList(this string str)
    {
        return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
            .ToList();
    }
    public static List<int> SplittedStringToInt32List(this string str)
    {
        return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
    }
    #endregion
    #region エラトステネスの篩
    public static List<int> GeneratePrimes(int max)
    {

        var range = Enumerable.Range(2, max - 1).ToList();
        var count = range.Count();
        var ls = new List<int>();
        while (true)
        {
            int num = 0;
            try
            {
                num = range.First();
                ls.Add(num);
            }
            catch
            {
                break;
            }
            for (int i = 1; i * num <= max; i++)
            {
                range.Remove(i * num);
            }
            if (range.Count <= 0)
            {
                break;
            }
        }
        return ls;
    }
    #endregion
    #region DeepClone
    public static T DeepClone<T>(this T src)
    {
        using (var memoryStream = new MemoryStream())
        {
            var binaryFormatter
              = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, src);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return (T)binaryFormatter.Deserialize(memoryStream);
        }
    }
    #endregion
    #endregion
}



Measured memory_bytes: 10820