Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: #!python
# -*- coding: utf-8 -*-

import sys

import numpy as np

sys.setswitchinterval(10**8)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if not map_[y, x]:
        return

    map_[y, x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break

    map_ = np.array([list(map(int, input().split())) for h in range(H)])
    # print(map_)
    island_num = 0
    while np.amax(map_):
        x = np.argmax(map_) % W
        y = np.argmax(map_) // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print('\t', island_num)

Python: import sys

sys.setrecursionlimit(10**7)

while(True):
    # w, h <= 50
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break

    # 0=>海, 1=>陸
    island = [list(input().split()) for _ in range(h)]

    island_number = 0

    def dfs(y, x):
        if x < 0 or w <= x or y < 0 or h <= y:
            return

        if island[y][x] == "0":
            return

        island[y][x] = "0"

        dfs(y, x - 1)
        dfs(y, x + 1)
        dfs(y - 1, x)
        dfs(y + 1, x)
        dfs(y + 1, x + 1)
        dfs(y + 1, x - 1)
        dfs(y - 1, x + 1)
        dfs(y - 1, x - 1)



    # iは横軸 jは縦軸
    for i in range(w):
        for j in range(h):

            if island[j][i] == "1":
                dfs(j, i)
                island_number += 1

    print(island_number)

Python: from collections import deque

move = [(0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1), (1, 0), (-1, 0)]

def count(chizu, start):
    stack = deque([start])
    while stack:
        y, x = stack.pop()
        for dxi, dyi in move:
            if not (0<=x+dxi<w and 0<=y+dyi<h) or chizu[y+dyi][x+dxi] == "0":
                continue
            else:
                stack.append((y+dyi, x+dxi))
        chizu[y][x] = "0"


while True:
    w, h = map(int,input().split())
    if w == 0:
        break
    
    island = [0]*h
    ans = 0
    for i in range(h):
        island[i] = list(input().split())
    
    for r in range(h):
        for c in range(w):
            if island[r][c] == "1":
                count(island, (r, c))
                ans += 1
    print(ans)

Python: #!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

D = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
def solve(w,h):
    def bfs(y,x):
        q = deque([(y,x)])
        while q:
            y,x = q.popleft()
            for dx,dy in D:
                ny,nx = y+dy,x+dx
                if 0 <= ny < h and 0 <= nx < w:
                    if c[ny][nx] and not d[ny][nx]:
                        d[ny][nx] = 1
                        q.append((ny,nx))
    c = LIR(h)
    d = [[0]*w for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] and not d[y][x]:
                ans += 1
                d[y][x] = 1
                bfs(y,x)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    while 1:
        w,h = LI()
        if w == h == 0:
            break
        solve(w,h)

Python: import sys
sys.setrecursionlimit(10**9)
def solve(w,h):
    G = [list(map(int,input().split())) for _ in range(h)]
    seen = [[False]*w for _ in range(h)]
    ans = 0
    def dfs(x,y):
        if seen[y][x]:
            return
        seen[y][x] = True
        for dx in range(-1,2):
            for dy in range(-1,2):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < w and 0 <= ny < h:
                    if G[y][x] == 1:
                        dfs(nx,ny)
    for x in range(w):
        for y in range(h):
            if G[y][x] == 1 and seen[y][x] == False:
                dfs(x,y)
                ans += 1
    print(ans)


while True:
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    solve(w,h)

Python: import sys

sys.setrecursionlimit(10000)

def ironuri(x, y, t, img,h,w):
    if img[y][x]!=1:
        return 0

    img[y][x]=t
    
    if y<h-1:
        ironuri(x,y+1,t,img,h,w)
    if y>0:
        ironuri(x,y-1,t,img,h,w)
    if x<w-1:
        ironuri(x+1,y,t,img,h,w)
    if x>0:
        ironuri(x-1,y,t,img,h,w)

    if x>0 and y>0:
        ironuri(x-1,y-1,t,img,h,w)
    if x>0 and y<h-1:
        ironuri(x-1,y+1,t,img,h,w)
    if x<w-1 and y>0:
        ironuri(x+1,y-1,t,img,h,w)
    if x<w-1 and y<h-1:
        ironuri(x+1,y+1,t,img,h,w)

    
    return 1
    
while True:
    cheese=[]
    il=2
    w,h=map(int, input().split())
    if w==0 and h==0:
        break
    
    for i in range(h):
        ci=list(map(int, input().split()))
        cheese.append(ci)
    
    for i in range(h):
        for j in range(w):
            if(ironuri(j,i,il,cheese,h,w)):
                il+=1

    print(il-2)