#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;

struct Scanner<I: Iterator<Item = char>> {
    iter: std::iter::Peekable<I>,
}

macro_rules! exit {
    () => {{
        exit!(0)
    }};
    ($code:expr) => {{
        if cfg!(local) {
            writeln!(std::io::stderr(), "===== Terminated =====")
                .expect("failed printing to stderr");
        }
        std::process::exit($code);
    }}
}

impl<I: Iterator<Item = char>> Scanner<I> {
    pub fn new(iter: I) -> Scanner<I> {
        Scanner {
            iter: iter.peekable(),
        }
    }

    pub fn safe_get_token(&mut self) -> Option<String> {
        let token = self.iter
            .by_ref()
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            Some(token)
        }
    }

    pub fn token(&mut self) -> String {
        self.safe_get_token().unwrap_or_else(|| exit!())
    }

    pub fn get<T: FromStr>(&mut self) -> T {
        self.token().parse::<T>().unwrap_or_else(|_| exit!())
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.get()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }

    pub fn char(&mut self) -> char {
        self.iter.next().unwrap_or_else(|| exit!())
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }

    pub fn line(&mut self) -> String {
        if self.peek().is_some() {
            self.iter
                .by_ref()
                .take_while(|&c| !(c == '\n' || c == '\r'))
                .collect::<String>()
        } else {
            exit!();
        }
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.iter.peek()
    }
}

trait Joinable {
    fn join(self, sep: &str) -> String;
}

impl<U: ToString, T: Iterator<Item = U>> Joinable for T {
    fn join(self, sep: &str) -> String {
        self.map(|x| x.to_string()).collect::<Vec<_>>().join(sep)
    }
}

struct Solver {
    h: usize,
    w: usize,
    visited: HashSet<(usize, usize)>,
}

impl Solver {
    fn new(h: usize, w: usize) -> Solver {
        Solver {
            h: h,
            w: w,
            visited: HashSet::new(),
        }
    }

    fn dfs(&mut self, adj: &Vec<Vec<usize>>, x: usize, y: usize) {
        if self.visited.contains(&(x, y)) {
            return;
        }
        self.visited.insert((x, y));

        let y = y as i64;
        let x = x as i64;
        for ny in y-1..y+2 {
            for nx in x-1..x+2 {
                if nx == x && ny == y { continue; }
                if nx < 0 || nx >= self.w as i64 || ny < 0 || ny >= self.h as i64 { continue; }
                if adj[y as usize][x as usize] == 1 {
                    self.dfs(adj, nx as usize, ny as usize);
                }
            }
        }
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
    loop {
        let w: usize = sc.get();
        let h: usize = sc.get();
        if w == 0 && h == 0 {
            break;
        }
        let a: Vec<Vec<usize>> = sc.mat(h, w);
        let mut s = Solver::new(h, w);
        let mut ans = 0;
        for y in 0..h {
            for x in 0..w {
                if s.visited.contains(&(x, y)) { continue; }
                if a[y][x] == 0 { continue; }
                s.dfs(&a, x, y);
                ans += 1;
            }
        }
        println!("{}", ans);
    }
}



Measured memory_bytes: 5352