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
sys.setrecursionlimit(10000)

def place(i, j, w):
    return i*w + j

def get_path(w, h, c):
    path = list()
    for i in range(h):
        for j in range(w):
            # place = i*w + j
            can_go = list()
            if c[i][j] != 0:
                if i != 0 and c[i-1][j] != 0:
                    can_go.append( place(i-1, j, w) )
                
                if i != h-1 and c[i+1][j] != 0:
                    can_go.append( place(i+1, j, w) )

                if j != 0 and c[i][j-1] != 0:
                    can_go.append( place(i, j-1, w) )
                
                if j != w-1 and c[i][j+1] != 0:
                    can_go.append( place(i, j+1, w) )
                
                if i != 0 and j!= 0 and c[i-1][j-1] != 0:
                    can_go.append( place(i-1, j-1, w) )
                
                if i != 0 and j != w-1 and c[i-1][j+1] != 0:
                    can_go.append( place(i-1, j+1, w) )
                
                if i!= h-1 and j != 0 and c[i+1][j-1] != 0:
                    can_go.append( place(i+1, j-1, w) )
                
                if i!= h-1 and j != w-1 and c[i+1][j+1] != 0:
                    can_go.append( place(i+1, j+1, w) )
                
            path.append(can_go)
    
    return path

def dfs(p, path, visit, w, h, c):
    visit[p] = 1 # 来訪時刻の記録

    for nxt in path[p]: #繋がってる点の内
        if visit[nxt] == 0: # 未探索の場合には
            dfs(nxt, path, visit, w, h, c) # 先に進む
    
    return

# def print_path(i, j, path, w, h):
#     arr = path[i*w + j]
#     print("print path start")
#     for x in arr:
#         print(x//w, x%w)
#     print("print path fin")

def solve(w, h, c):
    path = get_path(w, h, c)
    visit = [0]*(w*h)

    # print_path(3, 18, path, w, h)
    ans = 0
    for place in range(w*h):
        i = place // w
        j = place % w
        if c[i][j] != 0 and visit[place] == 0:
            dfs(place, path, visit, w, h, c)
            # print(i, j)
            ans += 1
    
    print(ans)
    return 

w, h = map(int, input().split())
while w != 0 and h != 0:
    c = [list(map(int, input().split())) for _ in range(h)]
    solve(w, h, c)
    w, h = map(int, input().split())

Python: from collections import deque

while True:
    w,h=map(int,input().split())
    if (w,h)==(0,0):
        break
    p=[]
    p.append([False for j in range(w+2)])
    for i in range(1,h+1):
        p.append([False]+[cij==1 for cij in list(map(int,input().split()))]+[False])
    p.append([False for j in range(w+2)])

    r=[[False for j in range(w+2)] for i in range(h+2)]
    ans=0
    for i in range(1,h+1):
        for j in range(1,w+1):
            if p[i][j] and not r[i][j]:
                q=deque()
                q.append((i,j));r[i][j]=True
                while q:
                    u=q.popleft()
                    ui,uj=u
                    for v in [(vi,vj) for vi in [ui-1,ui,ui+1] for vj in [uj-1,uj,uj+1]]:
                        vi,vj=v
                        if p[vi][vj] and not r[vi][vj]:
                            q.append(v);r[vi][vj]=True
                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 = [[c for c in 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.getrecursionlimit(10000)
    main()

Python: import sys
def f(n):
  print>>sys.stderr, n
  f(n+1)
f(1)

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 = [[c for c in 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(100000)
    main()

Python: import sys
sys.setrecursionlimit(4100000)

def dfs(x, y):
  atlas[y][x] = 0
  for horizonal in range(-1, 2):
    for vertical in range(-1, 2):
      nx = x + horizonal
      ny = y + vertical
      if nx < 0 or w <= nx or ny < 0 or h <= ny:
        continue
      if atlas[ny][nx] == 0:
        continue
      dfs(nx, ny)

while True:
  count = 0
  w, h = map(int, input().split())
  atlas = []
  if w == 0 and h == 0:
    break
  for _ in range(h):
    atlas.append(list(map(int, input().split())))
  for x in range(w):
    for y in range(h):
      if atlas[y][x] == 0:
        continue
      dfs(x, y)
      count += 1
  print(count)