using System;
using System.Collections.Generic;

namespace V1160{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var dx = new[]{0, 1, 0, -1, 1, 1, -1, -1};
            var dy = new[]{1, 0, -1, 0, 1, -1, 1, -1};

            while(true){
                var W = sr.Next<int>();
                var H = sr.Next<int>();
                var C = sr.Next<int>(H, W);
                if(W == 0 && H == 0) break;

                Func<int, int, bool> isInside = (x, y) => 0 <= x && x < W && 0 <= y && y < H;
                var res = 0;

                for(var i = 0; i < H; i++){
                    for(var j = 0; j < W; j++){
                        if(C[i][j] == 1){
                            res++;
                            C[i][j] = 0;

                            var que = new Queue<int>();
                            que.Enqueue(j);
                            que.Enqueue(i);

                            while(que.Count > 0){
                                var x = que.Dequeue();
                                var y = que.Dequeue();
                                for(var k = 0; k < 8; k++){
                                    var nx = x + dx[k];
                                    var ny = y + dy[k];
                                    if(isInside(nx, ny) && C[ny][nx] == 1){
                                        C[ny][nx] = 0;
                                        que.Enqueue(nx);
                                        que.Enqueue(ny);
                                    }
                                }
                            }
                        }
                    }
                }

                Console.WriteLine(res);
            }
            //---------------------------------
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}

Measured memory_bytes: 11508