using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;

public class B
{
	public static void Main() => (new Solver()).Solve();
}

public class Solver
{
	public void Solve()
	{
		var ans = new List<int>();
		while(true)
		{
			int W = GetInt();
			int H = GetInt();
			if(W == 0 && H == 0)
				break;

			ans.Add(solve(H, W));
		}
		WriteLine(string.Join("\n", ans));
	}

	private int solve(int H, int W)
	{
		var c = new int[H, W];
		for(int i = 0; i < H; i++)
		{
			for(int j = 0; j < W; j++)
				c[i, j] = GetInt();
		}

		int V = H * W;
		var uf = new UnionFind(V);
		var dy = new int[8] { -1, -1, -1, 0, 0, 1, 1, 1 };
		var dx = new int[8] { -1, 0, 1, -1, 1, -1, 0, 1 };
		for(int i = 0; i < H; i++)
		{
			for(int j = 0; j < W; j++)
			{
				if(c[i, j] == 0)
					continue;
				
				int v = i * W + j;
				for(int k = 0; k < 8; k++)
				{
					int ni = i + dy[k];
					int nj = j + dx[k];
					if(ni < 0 || ni >= H || nj < 0 || nj >= W || c[ni, nj] == 0)
						continue;

					int u = ni * W + nj;
					if(!uf.IsSame(v, u))
						uf.Merge(v, u);
				}
			}
		}

		var set = new HashSet<int>();
		for(int i = 0; i < H; i++)
		{
			for(int j = 0; j < W; j++)
			{
				if(c[i, j] == 1)
					set.Add(uf.Root(i * W + j));
			}
		}
		return set.Count;
	}
}

public class UnionFind
{
	private int[] par = null;
	private int[] rank = null;
	private int[] ccSize = null;
	
	public UnionFind(int n)
	{
		par = new int[n];
		rank = new int[n];
		ccSize = new int[n];
		for(int i = 0; i < n; i++)
		{
			par[i] = i;
			rank[i] = 0;
			ccSize[i] = 1;
		}
	}

	public bool IsSame(int x, int y)
	{
		return (Root(x) == Root(y));
	}

	public int Root(int x)
	{
		if(par[x] == x)
			return x;

		int r = Root(par[x]);
		par[x] = r;
		return r;
	}
	
	public bool Merge(int x, int y)
	{
		int rx = Root(x);
		int ry = Root(y);
		if(rx == ry)
			return false;
		
		if(rank[rx] < rank[ry])
		{
			int tmp = rx;
			rx = ry;
			ry = tmp;
		}
		else if(rank[rx] == rank[ry])
			rank[rx]++;

		par[ry] = rx;
		ccSize[rx] = ccSize[rx] + ccSize[ry];
		return true;
	}

	public int Size(int x)
	{
		return ccSize[Root(x)];
	}
}

public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => Enumerable.Range(0, (int)N).Select(_ => GetInt()).ToArray();
	public static long[] GetLongs(long N) => Enumerable.Range(0, (int)N).Select(_ => GetLong()).ToArray();
	public static double[] GetDoubles(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDouble()).ToArray();
	public static decimal[] GetDecimals(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDecimal()).ToArray();
	public static string[] GetStrings(long N) => Enumerable.Range(0, (int)N).Select(_ => GetString()).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) < 0)
			x = y;
	}

	public static void chmin<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) > 0)
			x = y;
	}

	public static void swap<T>(ref T x, ref T y)
	{
		T tmp = x;
		x = y;
		y = tmp;
	}
}


Measured memory_bytes: 16412