use std::cmp::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let lock = stdin.lock();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(lock));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

#[allow(unused_macros)]
macro_rules! max {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      max($x,max!($($xs)+))
    };
  }

#[allow(unused_macros)]
macro_rules! min {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      min($x,min!($($xs)+))
    };
  }

use std::ascii::AsciiExt;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::io::Write;
use std::iter::Iterator;

use std::fmt;
use std::ops::{Add, Div, Mul, Sub};

#[allow(dead_code)]
const MODLL: i64 = 1000_000_007;

#[derive(Copy, Clone)]
struct Modulo {
    n: i64,
}

// precondition: p is prime and mod
fn mod_inverse(a: i64, p: i64) -> i64 {
    if a == 1 {
        1
    } else {
        (1 - p * mod_inverse(p % a, a)) / a + p
    }
}

#[allow(dead_code)]
impl Modulo {
    fn new(n: i64) -> Modulo {
        Modulo {
            n: (n % MODLL + MODLL) % MODLL,
        }
    }

    fn inv(&self) -> Modulo {
        Modulo {
            n: mod_inverse(self.n, MODLL),
        }
    }
}

impl fmt::Debug for Modulo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.n)
    }
}

impl Add for Modulo {
    type Output = Modulo;
    fn add(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n + other.n) % MODLL,
        }
    }
}
impl Add<i32> for Modulo {
    type Output = Modulo;
    fn add(self, other: i32) -> Modulo {
        Modulo {
            n: (self.n + other as i64) % MODLL,
        }
    }
}
impl Add<i64> for Modulo {
    type Output = Modulo;
    fn add(self, other: i64) -> Modulo {
        Modulo {
            n: (self.n + other) % MODLL,
        }
    }
}
impl Sub for Modulo {
    type Output = Modulo;
    fn sub(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n - other.n + MODLL) % MODLL,
        }
    }
}
impl Sub<i32> for Modulo {
    type Output = Modulo;
    fn sub(self, other: i32) -> Modulo {
        self - Modulo::new(other as i64)
    }
}
impl Sub<i64> for Modulo {
    type Output = Modulo;
    fn sub(self, other: i64) -> Modulo {
        self - Modulo::new(other)
    }
}
impl Mul for Modulo {
    type Output = Modulo;
    fn mul(self, other: Modulo) -> Modulo {
        Modulo {
            n: (self.n * other.n) % MODLL,
        }
    }
}
impl Mul<i32> for Modulo {
    type Output = Modulo;
    fn mul(self, other: i32) -> Modulo {
        self * Modulo::new(other as i64)
    }
}
impl Mul<i64> for Modulo {
    type Output = Modulo;
    fn mul(self, other: i64) -> Modulo {
        self * Modulo::new(other)
    }
}
impl Div for Modulo {
    type Output = Modulo;
    fn div(self, other: Modulo) -> Modulo {
        self * other.inv()
    }
}
impl Div<i32> for Modulo {
    type Output = Modulo;
    fn div(self, other: i32) -> Modulo {
        self * mod_inverse(other as i64, MODLL)
    }
}
impl Div<i64> for Modulo {
    type Output = Modulo;
    fn div(self, other: i64) -> Modulo {
        self * mod_inverse(other, MODLL)
    }
}

struct Combination {
    fact: Vec<Modulo>,
    ifact: Vec<Modulo>,
}

#[allow(dead_code)]
impl Combination {
    fn new(n: usize) -> Combination {
        let mut fact = vec![Modulo::new(1); n + 1];
        let mut ifact = vec![Modulo::new(1); n + 1];
        for i in 1..n + 1 {
            fact[i] = fact[i - 1] * i as i64;
            ifact[i] = ifact[i - 1] / i as i64;
        }
        Combination {
            fact: fact,
            ifact: ifact,
        }
    }

    /// n!
    fn fact(&self, n: usize) -> Modulo {
        self.fact[n]
    }
    /// 1/n!
    fn inv_fact(&self, n: usize) -> Modulo {
        self.ifact[n]
    }
    /// nCr
    fn comb(&self, n: usize, r: usize) -> Modulo {
        if r > n {
            Modulo::new(1)
        } else {
            self.fact(n) * self.inv_fact(n - r) * self.inv_fact(r)
        }
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct UnionFindNode {
    pub par: usize,
    pub rank: usize,
}

#[derive(PartialEq, Debug, Clone)]
pub struct UnionFind(pub Vec<UnionFindNode>);

impl UnionFind {
    //初期化
    pub fn new(size: usize) -> UnionFind {
        let mut vec = Vec::new();
        for i in 0..size {
            vec.push(UnionFindNode { par: i, rank: 0 });
        }

        UnionFind(vec)
    }

    //根を求める
    pub fn find(&mut self, x: usize) -> usize {
        if self.0[x].par == x {
            x
        } else {
            let par = self.0[x].par;
            let v = self.find(par);
            self.0[x].par = v;
            v
        }
    }

    //xとyの集合を併合
    pub fn unite(&mut self, x: usize, y: usize) {
        let x = self.find(x);
        let y = self.find(y);
        if x == y {
            return;
        }

        if self.0[x].rank < self.0[y].rank {
            self.0[x].par = y;
        } else {
            self.0[y].par = x;
            if self.0[x].rank == self.0[y].rank {
                self.0[x].rank += 1;
            }
        }
    }

    //xとyが同じ集合に属するか
    pub fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }
}

pub trait LexicalPermutation {
    fn next_permutation(&mut self) -> bool;
    fn prev_permutation(&mut self) -> bool;
}

impl<T> LexicalPermutation for [T]
where
    T: Ord,
{
    fn next_permutation(&mut self) -> bool {
        if self.len() < 2 {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] >= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        let mut j = self.len() - 1;
        while j >= i && self[j] <= self[i - 1] {
            j -= 1;
        }
        self.swap(j, i - 1);
        self[i..].reverse();
        true
    }
    fn prev_permutation(&mut self) -> bool {
        if self.len() < 2 {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] <= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        self[i..].reverse();
        let mut j = self.len() - 1;
        while j >= i && self[j - 1] < self[i - 1] {
            j -= 1;
        }
        self.swap(i - 1, j);
        true
    }
}

//(小さい,同じ,大きい)
pub fn bound_count<T: Ord>(vec: &Vec<T>, val: &T) -> (usize, usize, usize) {
    let lb = lower_bound(vec, 0, vec.len(), val);
    let ub = upper_bound(vec, 0, vec.len(), val);
    (lb, ub - lb, vec.len() - ub)
}

//より小さい
pub fn less_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    lower_bound(vec, 0, vec.len(), val)
}

//より大きい
pub fn larger_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    vec.len() - upper_bound(vec, 0, vec.len(), val)
}

//同じ
pub fn eq_count<T: Ord>(vec: &Vec<T>, val: &T) -> usize {
    upper_bound(vec, 0, vec.len(), val) - lower_bound(vec, 0, vec.len(), val)
}

//vecは昇順ソート済み
//以上
pub fn lower_bound<T: Ord>(vec: &Vec<T>, mut first: usize, mut last: usize, val: &T) -> usize {
    let mut mid;
    while last - first > 1 {
        mid = (first + last) / 2;
        if &vec[mid] < val {
            first = mid;
        } else {
            last = mid;
        }
    }
    if &vec[first] < val {
        last
    } else {
        first
    }
}

//より大きい
pub fn upper_bound<T: Ord>(vec: &Vec<T>, mut first: usize, mut last: usize, val: &T) -> usize {
    let mut mid;
    while last - first > 1 {
        mid = (first + last) / 2;
        if &vec[mid] <= val {
            first = mid;
        } else {
            last = mid;
        }
    }
    if &vec[first] <= val {
        last
    } else {
        first
    }
}

use std::io::*;
use std::str::*;

fn read<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let s = stdin.bytes().map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok()
}


fn main() {


    let mut vec_ans = vec![];

    loop {

        let  w : usize = read().unwrap();
        let  h : usize = read().unwrap();

        if w == 0 && h == 0 {
            break;
        }

        let mut map = vec![vec![0; w]; h];
        for i in 0..h {
            for j in 0..w {
                map[i][j] = read().unwrap();
            }
        }

        let mut used = vec![vec![false; w]; h];
        let mut cnt = 0;
        for i in 0..h {
            for j in 0..w {
                if !used[i][j] && map[i][j] == 1 {
                    cnt += 1;
                    dfs(&map, &mut used, (j as i64, i as i64), w, h);
                }
            }
        }
        vec_ans.push(cnt);
    }

    for elem in vec_ans {
        println!("{}", elem);
    }
}

fn dfs(map: &Vec<Vec<usize>>, used: &mut Vec<Vec<bool>>, current: (i64, i64), w: usize, h: usize) {
    let x = vec![1, 1, 1, 0, 0, -1, -1, -1];
    let y = vec![1, 0, -1, 1, -1, 1, 0, -1];

    if used[current.1 as usize][current.0 as usize] {
        return;
    }
    used[current.1 as usize][current.0 as usize] = true;

    for i in 0..8 {
        let (nx, ny) = (current.0 + x[i], current.1 + y[i]);
        if nx >= 0
            && ny >= 0
            && nx < w as i64
            && ny < h as i64
            && map[ny as usize][nx as usize] == 1
        {
            dfs(map, used, (nx, ny), w, h);
        }
    }
}



Measured memory_bytes: 5260