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: # -*- coding: utf-8 -*-
import sys
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()


def main():
    while True:
        W,H=map(int,input().split())
        if W==H==0:
            exit()
        C=[[] for _ in range(H+2)]
        C[0]=C[H+1]=[-1]*(W+2)
        used=[[False]*(W+2) for _ in range(H+2)]
        rands=[]
        for column in range(1,H+1):
            C[column]=[-1]+list(map(int,input().split()))+[-1]
            for row,x in enumerate(C[column]):
                if x==1:
                    rands.append((column,row))

        ans=0
        
        if rands:
            s=[]
            while rands:
                y,x=rands.pop()
                if used[y][x]:
                    continue
                s.append((y,x))
                while s:
                    y,x=s.pop()
                    if used[y][x]:
                        continue
                    used[y][x]=True
                    for dy in range(-1,2):
                        for dx in range(-1,2):
                            ny,nx=y+dy,x+dx
                            if dy==dx==0 or used[ny][nx]:
                                continue
                            if C[ny][nx]==1:
                                s.append((ny,nx))
                ans+=1
        print(ans)
                

if __name__ == '__main__':
    main()

Python: import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    while True:
        w, h = map(int, input().split())
        if w==h==0:
            break
        c = [list(map(int, input().split())) for _ in range(h)]

        stk = []
        visited = [[False]*w for _ in range(h)]
        dir = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1))

        ans = 0

        for i in range(h):
            for j in range(w):
                if c[i][j]==1 and not visited[i][j]:
                    stk.append((i, j))
                    while len(stk)>0:
                        a = stk.pop()
                        visited[a[0]][a[1]] = True
                        for d in dir:
                            ad = (a[0]+d[0], a[1]+d[1])
                            if 0<=ad[0]<h and 0<=ad[1]<w and c[ad[0]][ad[1]]==1 and not visited[ad[0]][ad[1]]:
                                stk.append(ad) 
                    ans += 1

        print(ans)

if __name__ == '__main__':
    resolve()

Python: 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: import sys
from itertools import product

sys.setrecursionlimit(10000)

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

    m = tuple([list(map(int, input().split(' '))) for _ in range(H)])


    def dfs(y, x):
        if m[y][x] == 0:
            return False
        else:
            m[y][x] = 0

        for dy, dx in product((-1, 0, 1), repeat=2):
            ny = y + dy
            nx = x + dx
            if 0 <= ny < H and 0 <= nx < W and m[ny][nx] == 1:
                dfs(ny, nx)

        return True


    count = 0
    for i in range(H):
        for j in range(W):
            if m[i][j] == 1:
                count += int(dfs(i, j))

    print(count)