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**7) 

dx = [1,0,-1,0,1,-1,1,-1]
dy = [0,1,0,-1,1,1,-1,-1]

def dfs(C,h,w):
    global seen
    seen[h][w] = True
    for dir in range(8):
        nh = h + dx[dir]
        nw = w + dy[dir]
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if C[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(C,nh,nw)
    
while True:
    W,H = map(int, input().split())
    if W==0 and H==0:
        break
    C = []
    for i in range(H):
        array = list(map(int, input().split()))
        C.append(array)
    
    count = 0
    seen = [[False]*W for _ in range(H)]
    for dh in range(H):
        for dw in range(W):
            if C[dh][dw] != 0 and seen[dh][dw] == False:
                dfs(C,dh,dw)
                count += 1
    
    print(count)

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: class UnionSet:
    def __init__(self, nmax):
        self.size = [1]*nmax
        self.id = [i for i in range(nmax+1)]
    def root(self, i):
        while i != self.id[i]:
            self.id[i] = self.id[self.id[i]]
            i = self.id[i]
        return i
    def connected(self, p, q): return self.root(p) == self.root(q)
    def unite(self, p, q):
        i, j = self.root(p), self.root(q)
        if i == j: return
        if self.size[i] < self.size[j]:
            self.id[i] = j
            self.size[j] += self.size[i]
        else:
            self.id[j] = i
            self.size[i] += self.size[j]
# UNION-FIND library
 
def id(r, c):
    global w
    return r*w+c
 
mv = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
 
while True:
    w, h = map(int, input().split())
    if w == 0: break
    arr = [[] for r in range(h)]
    u = UnionSet(w*h)
    for r in range(h): arr[r] = list(map(int, input().split()))
    for r in range(h):
        for c in range(w):
            if not arr[r][c]: continue
            for i in range(8):
                r2, c2 = r + mv[i][0], c + mv[i][1]
                if r2 < 0 or r2 >= h or c2 < 0 or c2 >= w or not arr[r2][c2]: continue
                u.unite(id(r, c), id(r2, c2))
    ans = 0
    for r in range(h):
        for c in range(w):
            if arr[r][c] and u.root(id(r, c)) == id(r, c): ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10 ** 5)
anslist = []
def sarch(i, j, visited):

    if 0 < i:
        if visited[i - 1][j] == 0:
            visited[i - 1][j] = 1
            sarch(i - 1, j, visited)
        if 0 < j:
            if visited[i - 1][j - 1] == 0:
                visited[i - 1][j - 1] = 1
                sarch(i - 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i - 1][j + 1] == 0:
                visited[i - 1][j + 1] = 1
                sarch(i - 1, j + 1, visited)

    if i < len(visited) - 1:
        if visited[i + 1][j] == 0:
            visited[i + 1][j] = 1
            sarch(i + 1, j, visited)
        if 0 < j:
            if visited[i + 1][j - 1] == 0:
                visited[i + 1][j - 1] = 1
                sarch(i + 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i + 1][j + 1] == 0:
                visited[i + 1][j + 1] = 1
                sarch(i + 1, j + 1, visited)

    if 0 < j:
        if visited[i][j - 1] == 0:
            visited[i][j - 1] = 1
            sarch(i, j - 1, visited)
    if j < len(visited[0]) - 1:
        if visited[i][j + 1] == 0:
            visited[i][j + 1] = 1
            sarch(i, j + 1, visited)
    return visited


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    grid = [list(map(int, input().split())) for j in range(h)]
    visited = [[0 for i in range(w)] for j in range(h)]
    starti = 0
    startj = 0
    for i in range(h):
        for j in range(w):
            if grid[i][j] == 0:
                visited[i][j] = -1
            else:
                visited[i][j] = 0

    i = 0
    j = 0
    cnt = 0
    ans = 0
    #print(visited)
    while True:
        if i >= h:
            i = 0
            j = 0
        while True:
            cnt += 1
            if j >= w:
                j = 0
                break
            #print(visited[i][j], 100100)
            if visited[i][j] == 0:
                ans += 1
                visited[i][j] = 1
                visited = sarch(i, j, visited)
                cnt = 0
            j += 1
        i += 1
        #print(cnt, visited)
        if cnt > w * h:
            #print(10)
            break
    anslist.append(ans)

for i in anslist:
    print(i)

Python: def island(w,h,M,i,j):
    q=[(i,j)]
    while len(q)>0:
        cur=q.pop()
        M[cur[0]][cur[1]]=0
        for k1 in range(-1,2):
            for k2 in range(-1,2):
                if 0<=cur[0]+k1<h and 0<=cur[1]+k2<w:
                    if M[cur[0]+k1][cur[1]+k2]==1:
                        q.append(tuple((cur[0]+k1,cur[1]+k2)))


def solve(w,h):
    count=0
    M=[list(map(int,input().split())) for _2 in range(h)]
    for i in range(h):
        for j in range(w):
            if M[i][j]==1:
                count += 1
                island(w,h,M,i,j)
    return count
                

w,h=map(int,input().split())
while(w>0):
    print(solve(w,h))
    w,h=map(int,input().split())