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: import sys
sys.setrecursionlimit(10 ** 6)

def dfs(here_w, here_h):
    if 0 <= here_w < width and 0 <= here_h < height:
        if target[here_h][here_w]:
            target[here_h][here_w] = 0
            for x, y in all_move:
                dfs(here_w + x, here_h + y)
                
all_move = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
while True:
    width, height = map(int, input().split(" "))
    if not width:
        break
    target = []
    for h in range(height):
        temp = [int(n) for n in input().split(" ")]
        target.append(temp)
    # print(width, height, temp)
    answer = 0
    for h in range(height):
        for w in range(width):
            if target[h][w]:
                answer += 1
                dfs(w, h)
    print(answer)

Python: UNSEARCHED = 0
SEARCHED = -1
LAND = 1
SEA = 0
result = []

while(True):
    str = input().split(" ")
    w = int(str[0])
    h = int(str[1])
    if w == 0 and h == 0:
        break
    area = [[0 for i in range(w)] for i in range(h)]
    status = [[UNSEARCHED for i in range(w)] for i in range(h)]
    index = 0
    
    for i in range(h):
        str = input().split(" ")
        area[i] = [int(str[l]) for l in range(w)]
    
    for i in range(h):
        for l in range(w):
            if status[i][l] == UNSEARCHED and area[i][l] == LAND:
                index += 1
                status[i][l] = index
    
                # (y,x)
                stack = [(i+1, l), (i-1, l), (i, l+1), (i, l-1), (i+1, l+1), (i+1, l-1), (i-1, l-1), (i-1, l+1)]
                while(len(stack) > 0):
                    s = stack.pop(0)
                    
                    if not (0 <= s[0] < h and 0 <= s[1] < w):
                        continue
    
                    if status[s[0]][s[1]] == UNSEARCHED and area[s[0]][s[1]] == LAND :
                        status[s[0]][s[1]] = index
                        stack.append((s[0], s[1]))
                        stack.append((s[0], s[1]+1))
                        stack.append((s[0], s[1]-1))
                        stack.append((s[0]+1, s[1]))
                        stack.append((s[0]-1, s[1]))
                        stack.append((s[0]+1, s[1]+1))
                        stack.append((s[0]+1, s[1]-1))
                        stack.append((s[0]-1, s[1]+1))
                        stack.append((s[0]-1, s[1]-1))
                    elif area[s[0]][s[1]] == SEA:
                        status[s[0]][s[1]] = SEARCHED
    
            elif status[i][l] == UNSEARCHED:
                status[i][l] = SEARCHED
    
    result.append(index)

for i in result:
    print(i)

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


def search(n, m, tile, N, M):
    for dy, dx in zip([-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]):
        if 0 <= dy + n < N and 0 <= dx + m < M:
            if tile[dy + n][dx + m] == '1':
                tile[dy + n][dx + m] = '2'
                search(dy + n, dx + m, tile, N, M)


while True:
    M, N = map(int, input().split())
    if M == 0 and N == 0:
        break
    tile = [input().split() for n in range(N)]
    ans = 0
    for n in range(N):
        for m in range(M):
            if tile[n][m] == '1':
                tile[n][m] = '2'
                search(n, m, tile, N, M)
                ans += 1
    print(ans)

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [input().split() for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.setrecursionlimit(10000)
    main()

Python: # coding: utf-8
import sys
sys.setrecursionlimit(2147483647)
def check(p):
    global field
    if(field[p[1]][p[0]]=='1'):
        field[p[1]][p[0]]='0'
    if(0<=p[0]+1<w and field[p[1]][p[0]+1]=='1'):
        check((p[0]+1,p[1]))
    if(0<=p[0]-1<w and field[p[1]][p[0]-1]=='1'):
        check((p[0]-1,p[1]))
    if(0<=p[1]+1<h and field[p[1]+1][p[0]]=='1'):
        check((p[0],p[1]+1))
    if(0<=p[1]-1<h and field[p[1]-1][p[0]]=='1'):
        check((p[0],p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]+1]=='1'):
        check((p[0]+1,p[1]-1))
    if(0<=p[0]-1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]-1]=='1'):
        check((p[0]-1,p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]+1]=='1'):
        check((p[0]+1,p[1]+1))
    if(0<=p[0]-1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]-1]=='1'):
        check((p[0]-1,p[1]+1))
     
while 1:
    ans=0
    w,h=map(int,input().split())
    if w==0:
        break
    field=[]
    for i in range(h):
        s=''.join(input().split())
        field.append(list(s))
    for i in range(h):
        for j in range(w):
            if field[i][j]=='1':
                check((j,i))
                ans+=1
    print(ans)

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


def dfs(h, w):
    board[h][w] = 0
    for dh in range(-1, 2):
        for dw in range(-1, 2):
            nh = h + dh
            nw = w + dw
            if 0 <= nh < H and 0 <= nw < W and board[nh][nw] == 1:
                dfs(nh, nw)


while True:
    W, H = map(int, input().split())
    if H == 0 and W == 0:
        break
    board = [list(map(int, input().split())) for _ in range(H)]
    ans = 0
    for h in range(H):
        for w in range(W):
            if board[h][w]:
                dfs(h, w)
                ans += 1
    print(ans)