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: import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)


def search(i, j):
    global ans
    if c[i][j] == 1:
        c[i][j] = -1
        search(i, j+1)
        search(i+1, j-1)
        search(i+1, j)
        search(i+1, j+1)
        search(i, j-1)
        search(i-1, j)
        search(i-1, j+1)
        search(i-1, j-1)


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[] for _ in range(h + 2)]
    c[0] = [0] * (w + 2)
    c[-1] = [0] * (w + 2)
    for i in range(h):
        c[i + 1] = [0] + list(map(int, input().split())) + [0]
    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i][j] == 1:
                ans += 1
                search(i, j)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dx,dy in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
        search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(h, w, field, seen, dhw):
    seen[h][w] = True
    for dh, dw in dhw:
        nh = h + dh
        nw = w + dw

        if field[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(nh, nw, field, seen, dhw)


def main():
    res = []
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        field = []
        field.append([0] * (w + 2))
        for i in range(h):
            line = list(map(int, input().split()))
            field.append([0] + line + [0])
        field.append([0] * (w + 2))

        seen = [[False] * (w + 2) for x in range(h + 2)]
        dhw = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1)]

        count = 0
        for ih in range(1, h+2):
            for iw in range(1, w+2):
                if seen[ih][iw]:
                    continue
                if field[ih][iw] == 1:
                    dfs(ih, iw, field, seen, dhw)
                    count += 1
        res.append(count)

    for i in res:
        print(i)


main()

Python: import sys
sys.setrecursionlimit(10000)
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]


def dfs(y, x):
    if x < 0 or w <= x:
        return
    if y < 0 or h <= y:
        return
    if V[y][x]:
        return
    if C[y][x] == 0:
        return

    V[y][x] = True
    for i in range(8):
        dfs(y + dy[i], x + dx[i])


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    C = [list(map(int, input().split())) for _ in range(h)]
    V = [[False]*w for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if C[y][x] == 0:
                continue
            if V[y][x]:
                continue
            dfs(y, x)
            ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10**9)
H = 0
W = 0
field = []
def dfs(x, y, px, py):
    for dx, dy in ((1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)):
        dx += x
        dy += y
        if dx == px and dy == py: continue
        if not (0 <= dx < W and 0 <= dy < H): continue
        if field[dy][dx] == 1:
            field[dy][dx] = 2
            dfs(dx, dy, x, y)

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

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)