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: while 1:
    W, H = list(map(int,input().split()))
    if W == 0:break
 
    C = [list(map(int, input().split())) for i in range(H) ]
     
    def work(x,y):
        ax = [-1, 0, 1, -1, 1, -1, 0, 1]
        ay = [-1, -1, -1, 0, 0, 1, 1, 1]
 
        if C[y][x] != 1:return False
        stack = [(x,y)]
 
        while len(stack) > 0:
            s = stack.pop(-1)
            C[s[1]][s[0]] = -1 #mark
 
            for _x in [s[0] + _x for _x in ax]:
                for _y in [s[1] + _y for _y in ay]:
                    if 0 <= _x < W and 0 <= _y < H and C[_y][_x] == 1:
                        stack.append((_x, _y))
        return True
#    def work(x,y):
#        ax = [-1, 0, 1, -1, 1, -1, 0, 1]
#        ay = [-1, -1, -1, 0, 0, 1, 1, 1]
#
#        if C[y][x] != 1:return False
#        C[y][x] = -1 #mark
#
#        for _ax in ax:
#            for _ay in ay:
#                if 0 <= _ax + x < W and 0 <= _ay + y < H:
#                    work(_ax + x, _ay + y)
#        return True
 
    cnt = 0
    for x in range(W):
        for y in range(H):
            cnt += work(x,y)
    print(cnt)

Python: def solve():
    from collections import deque
    
    while True:
        w, h = map(lambda x: int(x)+2, input().split())
        if w == 2:
            break

        result = 0
        a = ['0']*w
        for _ in [0]*(h-2):
            a += ['0'] + input().split() + ['0']
        a += ['0']*w

        for i in range(w*h):
            if a[i] != '1':
                continue
            a[i] = '0'
            result += 1
            dq = deque([i])
            while dq:
                j = dq.popleft()
                for dest in (j-w-1, j-w, j-w+1, j-1, j+1, j+w-1, j+w, j+w+1):
                    if a[dest] == '1':
                        a[dest] = '0'
                        dq.append(dest)
        print(result)


if __name__ == "__main__":
    solve()

Python: import sys
sys.setrecursionlimit(10**9)
ans = []
dx = (1,1,1,0,0,-1,-1,-1)
dy = (1,0,-1,1,-1,1,0,-1)

def dfs(x,y):
    st[x][y] = True
    for i,j in zip(dx,dy):
        nx = x + i
        ny = y + j
        if nx < 0 or h <= nx: continue
        if ny < 0 or w <= ny: continue
        if st[nx][ny]: continue
        if c[nx][ny]: dfs(nx,ny)

while True:
    w,h = map(int,input().split())
    if w == 0 and h == 0: break
    c = [[int(i) for i in input().split()] for _ in range(h)]
    st = [[False]*w for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if st[i][j]: continue
            if c[i][j]:
                dfs(i,j)
                cnt += 1
    ans.append(cnt)

print("\n".join(map(str,ans)))

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
    
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
    
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                count += 1

    print(count)

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

try:
    import sys

    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 * W + x]:
            return

        map_[y * W + 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_ = []
        for h in range(H):
            map_ += list(map(int, input().split()))
        island_num = 0
        while any(map_):
            next_idx = map_.index(1)
            x = next_idx % W
            y = next_idx // W
            DFS(x, y)
            island_num += 1

        print(island_num)
except Exception as e:
    print(e)

Python: from collections import deque
def dfs(i,j,seen):
    q=deque()
    q.append((i,j,0))
    seen[i][j]=0
    dx=[0,1,0,-1,-1,-1,1,1]
    dy=[1,0,-1,0,1,-1,1,-1]
    while q:
        X,Y,S=q.pop()
        if S==0:
            #下に遷移していくときの処理
            #q.append((V,P,1))
            for k in range(8):
                nx=X+dx[k]
                ny=Y+dy[k]
                if not(0<=nx<h and 0<=ny<w):continue
                if seen[nx][ny]==0:continue
                seen[nx][ny]=0
                q.append((nx,ny,0))
    return 1

def main(w,h):
    grid=[list(map(int,input().split())) for i in range(h)]
    ans=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]:
                ans+=dfs(i,j,grid)
    print(ans)

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

Python: import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

def dfs(x,y):
    c[x][y]='0'
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx=dx+x
            ny=dy+y
            if c[nx][ny]=='1':dfs(nx,ny)

while 1:
    c=[]
    w,h=map(int,input().split())
    if w==h==0:exit()
    w+=2
    c=[list('@'*w)]
    for i in range(h):
        c+=[list('@'+input().replace(' ','').replace('\n','')+'@')]
    c+=[list('@'*w)]
    h+=2
    cnt=0

    for i in range(1,h):
        for j in range(1,w):
            if c[i][j]=='1':
                dfs(i,j)
                cnt+=1
    print(cnt)