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: while(1):
 w,h = map(int,input().split())
 if((w == 0)and(h == 0)): break

 Map = []
 Map.append(['@']*(w+2))
 land = []
 for i in range(h):
  c = list('@'+input().replace(' ', '')+'@')
  Map.append(c)
  for j in range(w+2):
   if(c[j] == '1'): land.append([i+1,j])
 Map.append(['@']*(w+2))
 #print(land)

 #深さ優先探索
 def dfs(start_x,start_y):
  stack = []
  stack.append([start_x,start_y])
  
  while(1):      
   #stackが空になると終了
   if(stack == []): return
  
   #座標の取り出し
   cord = stack.pop()
   x,y = cord[0],cord[1]
  
   #行き止まりor海なら何もしない
   if((Map[x][y] == '0')or(Map[x][y] == '@')): continue
   #訪問済みなら何もしない
   elif(vis[x][y] == 1): continue
   else:
    vis[x][y] = 1 #訪問済みに

   #次行くところ
   vec_x = [0,0,1,-1,1,-1,1,-1]
   vec_y = [1,-1,0,0,1,-1,-1,1]
   for i in range(8):
     if(Map[x+vec_x[i]][y+vec_y[i]] == '1'):
       stack.append([x+vec_x[i],y+vec_y[i]])    
  
 ans = 0
 vis = [[0]*(w+2) for _ in range(h+2)]
 for i in range(len(land)):
  start_x,start_y = land[i][0],land[i][1]
  if(vis[start_x][start_y] == 0):
      dfs(start_x,start_y)
      ans += 1
      #print('aaa')
 print(ans)

Python: from collections import deque
QUE=deque()


while True:
    w,h=map(int,input().split())
    
    if w==h==0:
        break
    
    MAP=[list(map(int,input().split())) for i in range(h)]
    ANSLIST=[[0 for i in range(w)] for j in range(h)]

    NOW=1

    for i in range(h):
        for j in range(w):
            if MAP[i][j]==0:
                continue
            if ANSLIST[i][j]!=0:
                continue
            
            QUE.append((i,j))
            while QUE:
                i,j=QUE.pop()
                for x,y in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]:
                    if 0<=x<h and 0<=y<w and MAP[x][y]==1 and ANSLIST[x][y]==0:
                        ANSLIST[x][y]=NOW
                        QUE.append((x,y))

            NOW+=1

    #for an in ANSLIST:
    #    print(an)

    print(NOW-1)

Python: import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect


# from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

def dfs(x, y):
    flg = False
    if data[x][y] == 0 or table[x][y] == 1:
        return False
    else:
        table[x][y] = 1
        flg = True


    for i in range(x - 1, x + 2):
        if i < 0 or i > h-1:
            continue
        for j in range(y - 1, y + 2):
            if j < 0 or j > w-1:
                continue
            if not(x == i and y == j):
                tmp = dfs(i, j)

    return flg


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break

    data = getSomeInputListInt(h)
    table = [[0 for _ in range(w)] for _ in range(h)]

    ans = 0
    for i in range(h):
        for j in range(w):
            if dfs(i, j):
                ans += 1

    print(ans)

Python: from collections import deque


def dfs(G, start):
    S = deque()
    child = deque()
    VL[start[1]][start[0]] = 1
    S.append(start)
    while len(S) > 0:
        node = S.pop()
        for i in range(len(dx)):
            child.clear()
            nx = node[0] + dx[i]
            ny = node[1] + dy[i]

            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue
            if G[ny][nx] == 0:
                continue
            if VL[ny][nx] == 1:
                continue

            child.append((nx, ny))

            if len(child) > 0:
                for c in child:
                    VL[c[1]][c[0]] = 1
                    S.append(c)


ok = True
while ok:
    W, H = map(int, input().split())

    if W == 0 and H == 0:
        break

    G = [list(map(int, input().split())) for _ in range(H)]
    VL = [[0 for _ in range(W)] for _ in range(H)]

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

    res = 0
    for i in range(H):
        for j in range(W):
            if G[i][j] == 1 and VL[i][j] == 0:
                dfs(G, (j, i))
                res += 1

    print(res)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    if 0<=x<w and 0<=y<h and a[y][x]=='1':
        a[y][x]='0'
        for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:f(x+dx,y+dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[input().split() for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)

Python: import sys
while True:
    sys.setrecursionlimit(10000000)
    w,h=map(int,input().split())
    if w==0 and h==0:
        sys.exit()

    field=[list(map(int,input().split())) for i in range(h)]

    def dfs(x,y):
        if not(0<=x<h) or not(0<=y<w):
            return
        elif field[x][y]==0:
            return
        field[x][y]=0
        for i in range(3):
            for j in range(3):
                dfs(x-1+i,y-1+j)
        return 0
    ans=0
    for i in range(h):
        for j in range(w):
            if field[i][j]==1:
                dfs(i,j)
                ans+=1     
    print(ans)