Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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

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()))
    # print(map_)
    island_num = 0
    while any(map_):
        next_idx = map_.index(1)
        x = next_idx % W
        y = next_idx // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print(island_num)

Python: import collections,itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
while 1:
    w,h = LI()
    if w==h==0:
        break
    ans = 0
    c = [LI() for _ in range(h)]
    seen = [[0]*w for _ in range(h)]
    stack = []
    dhdw = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
    def dfs(H,W):
        seen[H][W] = 1
        stack.append([H,W])
        while stack:
            sh,sw = stack.pop()
            for dh,dw in dhdw:
                nh,nw = sh+dh,sw+dw
                if not(0<=nh<=h-1) or not(0<=nw<=w-1) or seen[nh][nw] or c[nh][nw]==0:
                    continue
                seen[nh][nw] = 1
                stack.append([nh,nw])
    for H,W in itertools.product(range(h),range(w)):
        if seen[H][W] or c[H][W]==0:
            continue
        ans += 1
        dfs(H,W)
    print(ans)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**6)

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(len(dxdy)):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

Python: import sys
sys.setrecursionlimit(10**7)
def dfs(i, j):
    if(c[i][j] != "1"):
        return 1
    else:
        c[i][j] = "2"

        if(0 < i):                      dfs(i - 1, j)
        if(0 < j):                      dfs(i, j - 1)
        if(i < h - 1):                  dfs(i + 1, j)
        if(j < w - 1):                  dfs(i, j + 1)
        if(0 < i and 0 < j):            dfs(i - 1, j - 1)
        if(0 < i and j < w - 1):        dfs(i - 1, j + 1)
        if(i < h - 1 and 0 < j):        dfs(i + 1, j - 1)
        if(i < h - 1 and j < w - 1):    dfs(i + 1, j + 1)
        return 1

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

for i in range(len(ans)):
    print(ans[i])

Python: import sys

def landsearch(i,j):
    result=[]
    if i>0:
        if j>0:
            for x in range(-1,2):
                for y in range(-1,2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
        else:
            for x in range(-1,2):
                for y in range(2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
    elif j>0:
        for x in range(2):
            for y in range(-1,2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    else:
        for x in range(2):
            for y in range(2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    return result           


while True:
    w,h=[int(i) for i in input().split(" ")]
    if w==0 and h==0:
        sys.exit()


    atlas=[]
    for i in range(h):
        atlas.append([[int(j),0] for j in input().split(" ")])


    how_many_island=0
    for i in range(h):
        for j in range(w):
            if atlas[i][j][0]==1 and atlas[i][j][1]==0:
                how_many_island+=1
                dis_list=[[i,j]]
                while len(dis_list)!=0:
                    dis_list_cash=[]
                    for k in dis_list:
                        land=landsearch(k[0],k[1])
                        for l in land:
                            if atlas[l[0]][l[1]][1]==0:
                                atlas[l[0]][l[1]][1]=1
                                dis_list_cash.append([l[0],l[1]])
                    dis_list=dis_list_cash

    print(how_many_island)