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: import queue
def allvisited():
    for i in range(w):
        for j in range(h):
            if (visited[i][j]==False and s[i][j]=='1'):
                return (False,i,j)
    else:
         return (True,-1,-1)
        
def legal(pos):
    if(0<=pos[0]<w) and (0<=pos[1]<h) and s[pos[0]][pos[1]]=='1' and visited[pos[0]][pos[1]]==False:
        return True
    else:
        return False
    
d=[[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
while(1):
    h,w=map(eval,input().split())
    if (w==0 and h==0): break
    s=[]
    for i in range(w):
        s.append(input().split())
    #print(s)
    Q=queue.Queue()
    visited=[]
    for i in range(w):
        visited.append([False]*h)
    #print(visited)
    count=0
    while(1):
        a=allvisited()
        if (a[0]==False):
            Q.put((a[1],a[2]))
            count+=1
        else:
            break
        while(not Q.empty()):
            now=Q.get()
            visited[now[0]][now[1]]=True
            for i in range(8):
                new=(now[0]+d[i][0],now[1]+d[i][1])
                if(legal(new)):
                    Q.put(new)
    print(count)

Python: import sys
readline = sys.stdin.buffer.readline

from collections import deque
import copy

def myinput():
    return map(int,readline().split())

def mycol(data,col):
    return [ row[col] for row in data ]

ls_ans = []

w,h = myinput()
c = [ list(myinput()) for _ in range(h) ]

while w!=0 or h!=0:
    def dfs(sx,sy):
        stack = deque()
        stack.append([sx,sy])
        while stack:
            x,y = stack.pop()
            if c[x][y]==1:
                c[x][y] = 0
                for i in range(-1,2):
                    for j in range(-1,2):
                        if i==0 and j==0:
                            continue
                        else:
                            nx = x + i
                            ny = y + j
                            if nx==-1 or ny==-1 or nx==h or ny==w:
                                pass
                            else:
                                if c[nx][ny]==1:
                                    stack.append([nx,ny])
                                else:
                                    pass
            else:
                pass

    ans = 0
    for i in range(h):
        for j in range(w):
            if c[i][j]==1:
                dfs(i,j)
                ans += 1
            else:
                pass
    # print(ans)
    ls_ans.append(ans)

    w,h = myinput()
    c = [ list(myinput()) for _ in range(h) ]

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

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(map, reached, pos):
    global dx, dy
    x, y = pos

    if reached[x][y] == 1 or map[x][y] == '0':
        return 0

    reached[x][y] = 1
    for dxi, dyi in zip(dx, dy):
        if not reached[x + dxi][y + dyi]:
            dfs(map, reached, (x + dxi, y + dyi))

    return int(map[x][y])


def island(map):
    w = len(map)
    h = len(map[0])
    reached = [[0] * h for _ in range(w)]

    ans = 0
    for x in range(w):
        for y in range(h):
            if reached[x][y] == 1 or map[x][y] == '0':
                continue
            ans += dfs(map, reached, (x, y))

    return ans


dx = [1, 0, -1, 0, 1, -1, 1, -1]
dy = [0, 1, 0, -1, 1, -1, -1, 1]

while True:
    w, h = map(int, input().split())

    if (0, 0) == (w, h):
        break

    mapp = ['0' * (w + 2)]
    for _ in range(h):
        mapp.append('0%s0' % input().replace(' ', ''))
    mapp.append('0' * (w + 2))
    print(island(mapp))

Python: import sys

sys.setrecursionlimit(10000)

def dfs(pos):
    x = pos[0]
    y = pos[1]
    C[y][x] = -1
    
    for x_d, y_d in [[1,0],[1,1],[0,1],[1,-1],[-1,1],[-1,0],[-1,-1],[0,-1]]:        
        x_new = x+x_d
        y_new = y+y_d

        if not 0<=x_new<=w-1:
            continue

        if not 0<=y_new<=h-1:
            continue

        if C[y_new][x_new]!=1:
            continue

        dfs([x_new, y_new])

while True:
    w, h = map(int, input().split())
    if w==h==0:
        break
    
    C = [list(map(int, input().split())) for _ in range(h)]
    
    counter = 0
    for w_i in range(w):
        for h_i in range(h):
            
            if C[h_i][w_i]==1:
                counter += 1
                dfs([w_i,h_i])
    
    print(counter)

Python: import sys
def f(n):
  if n%100==0:
    print>>sys.stderr, n
  f(n+1)
f(1)

Python: import queue

def proceess(start_x,start_y):
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        #print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < w and ny < h:
                    #print(map[nx][ny])
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        #print("nx : " + str(nx) + ", ny : " + str(ny))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 1 and h == 1:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    #print(map)

    count = 0
    for i in range(w):
        for j in range(h):
            if map[i][j] == 1:
                #print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)