Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: from collections import deque
import sys
sys.setrecursionlimit(10000)

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr):
    first_ptr=first_ptr+1
#    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True 
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
    last_ptr=last_ptr+1
 #   first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr


while True:
    w,h=map(int,input().split())
    if w==0 and h==0:
        break
    c=[[0]*w for i in range(h)]
    for i in range(h):
        c[i]=list(map(int,input().split()))
    
    n=w*h
    g = [deque([]) for _ in range(n + 1)]
    for jh in range(h):
        for jw in range(w):
            if c[jh][jw]==0:
                continue
            i=w*jh+jw
#            print(jh,jw,i,c[jh][jw])
            if jh<h-1 :
                kh=jh+1
                kw=jw
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jh>0 :
                kh=jh-1
                kw=jw
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jw<w-1 :
                kh=jh
                kw=jw+1
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jw>0 :
                kh=jh
                kw=jw-1
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jw<w-1 and jh<h-1:
                kh=jh+1
                kw=jw+1
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jw>0 and jh>0:
                kh=jh-1
                kw=jw-1
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jw<w-1 and jh>0:
                kh=jh-1
                kw=jw+1
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
#                    g[ii].append(i)
            if jw>0 and jh<h-1:
                kh=jh+1
                kw=jw-1
                if c[kh][kw]==1:
                    ii=w*(kh)+kw
                    g[i].append(ii)
 #                   g[ii].append(i)

    seen=[False]*n
    first_ptr=0
    last_ptr=0
    first_order =[-1]*n
    last_order = [-1]*n

    icnt=0
    for jh in range(h):
        for jw in range(w):
            v=w*jh+jw
            if c[jh][jw]==1:
                if seen[v]:
                    continue
                else:
                    first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr)
                    icnt+=1

    print(icnt)

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

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(3):
        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: from collections import deque
ss = [0,1,0,-1]
dd = [1,0,-1,0]
while True:
    w,h = map(int ,input().split())
    if w == h == 0:
        break
    ll = []
    for i in range(h):
        ll.append(list(map(int, input().split())))
    cnt = 0
    passed = [[False]*w for i in range(h)]
    q = deque()
    for i in range(h):
        for j in range(w):
            if passed[i][j]:
                continue
            if ll[i][j] == 1:
                passed[i][j] = True
                q.append([i,j])
                cnt += 1
                while q:
                    l = q.popleft()
                    a,b = l[0], l[1]
                    for s in ss:
                        for d in dd:
                            if s == d == 0:
                                continue
                            ta = a + s
                            tb = b + d
                            if 0 <= ta <= h-1 and 0 <= tb <= w-1 and ll[ta][tb] == 1 and not passed[ta][tb]:
                                passed[ta][tb] = True
                                q.append([ta,tb])
    print(cnt)

Python: import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)

def main():
    while True:
        W, H = map(int, input().split())
        if W == 0:
            break
        board = [list(map(int, input().split())) for i in range(H)]
        check = [[False] * W for i in range(H)]

        def dfs(x, y):
            check[y][x] = True
            for nx, ny in [(x, y-1), (x+1, y-1), (x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1), (x-1, y), (x-1, y-1)]:
                if 0 <= nx and nx < W and 0 <= ny and ny < H:
                    if not check[ny][nx] and board[ny][nx] == 1:
                        dfs(nx, ny)

        ans = 0
        for y in range(H):
            for x in range(W):
                if not check[y][x] and board[y][x] == 1:
                    ans += 1
                    dfs(x, y)
        print(ans)


if __name__ == "__main__":
    main()

Python: import sys
sys.setrecursionlimit(10000)

def main():
    while 1:
        W, H = map(int, input().split())
        if W == 0 and H == 0:
            break
        C = [list(map(int, input().split())) for _ in range(H)]

        def dfs(h, w):
            C[h][w] = 0

            for dh in range(-1, 2):
                for dw in range(-1, 2):
                    nh = h + dh
                    nw = w + dw
                    if nh < 0 or nh >= H or nw < 0 or nw >= W:
                        continue
                    if C[nh][nw] == 0:
                        continue
                    dfs(nh, nw)

        count = 0
        for h in range(H):
            for w in range(W):
                if C[h][w] == 0:
                    continue
                dfs(h, w)
                count += 1
        print(count)


if __name__ == "__main__":
    main()