using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pair = System.Tuple<int, int>;
using Edge = System.Tuple<int, int, long>;
//using Debug;
//using static System.Globalization.CultureInfo;

class Program
{
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    static bool[,] th;
    static int[] wh;
    static int[][] gred;
    static void Solve()
    {
        var res = new List<int>();
        while (true)
        {
            wh = Input.ar;
            if (wh[0] == 0 && wh[1] == 0) break;
            var ct = 0;
            th = new bool[wh[1], wh[0]];
            gred = Enumerable.Repeat(0, wh[1]).Select(_ => Input.ar).ToArray();
            for(var i=0;i<wh[1]; i++)
                for(var j=0;j<wh[0];j++)
                    if (!th[i, j] && gred[i][j] == 1)
                    {
                        dfs(i, j);
                        ct++;
                    }
            res.Add(ct);
        }
        WriteLine(string.Join("\n", res));
    }
    static void dfs(int h,int w)
    {
        th[h, w] = true;
        for(var i=-1;i<2;i++)
            for(var j=-1;j<2;j++)
            {
                var hh = i + h;
                var ww = j + w;
                if (!Search.IsInside(ww, hh, wh)||th[hh,ww]||gred[hh][ww]==0) continue;
                dfs(hh, ww);
            }
    }
}

public class Input
{
    public static string read => ReadLine();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(v => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(v => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).Select(v => arL).ToArray();
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}

public class Search
{
    public static int UpperBound<T>(T[] array,T value)
        where T:IComparable<T>
    {
        var low = 0;
        var high = array.Length;
        while (high > low)
        {
            var mid = (high + low) / 2;
            if (array[mid].CompareTo(value) == 1) high = mid;
            else low = mid + 1;
        }
        return low;
    }

    public static int LowerBound<T>(T[] array,T value)
        where T : IComparable<T>
    {
        var low = 0;
        var high = array.Length;
        while (high > low)
        {
            var mid = (high + low) / 2;
            if (array[mid].CompareTo(value) != -1) high = mid;
            else low = mid + 1;
        }
        return low;
    }

    public static T Max<T>(params T[] array)
        => array.Max();
    public static T Min<T>(params T[] array)
        => array.Min();
    public static bool IsInside(int h, int w, int[] hw)
        => 0 <= h && h < hw[0] && 0 <= w && w < hw[1];
    public static bool IsPrime(long num)
    {
        if (num % 2 == 0 || num == 1) return num == 2;
        for (var i = 3; i * i <= num; i += 2)
            if (num % i == 0) return false;
        return true;
    }
}



Measured memory_bytes: 18460