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: #!/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(4):
        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: import sys
 
sys.setrecursionlimit(10**6)


def lake_counting(field):

  lake_count = 0

  def dfs(x,y):
    field[x][y] = 0
    dx = [-1,-1,-1,0,0,1,1,1]
    dy = [-1,0,1,-1,1,1,0,-1]
    

    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < h and 0 <= ny < w and field[nx][ny] == 1):
          dfs(nx,ny)

  for i in range(h):
    for j in range(w):
      if field[i][j] == 1:
        dfs(i,j)
        lake_count += 1

  return lake_count

while True:
  w, h = map(int,input().split())
  if w == h == 0:
    break
  A = []
  for i in range(h):
    c = list(map(int,input().split()))
    A.append(c)
  print(lake_counting(A))

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

        c = [[0] * (w+2)]
        for _ in range(h):
            c.append([0] + [int(i) for i in readline().split()] + [0])
        c.append([0] * (w+2))

        cnt = 0
        seen = [[-1] * (w+2) for _ in range(h+2)]
        def dfs(sx, sy):
            stack = [(sx, sy)]
            
            while stack:
                x, y = stack.pop()

                for dx in [-1, 0, 1]:
                    for dy in [-1, 0, 1]:
                        if dx == 0 and dy == 0:
                            continue
                        nx = x + dx
                        ny = y + dy

                        if c[nx][ny] and seen[nx][ny] == -1:
                            seen[nx][ny] = 1
                            stack.append((nx, ny))

        for i in range(1,h+1):
            for j in range(1,w+1):
                if c[i][j] and seen[i][j] == -1:
                    cnt += 1
                    dfs(i, j)

        ans.append(cnt)
    
    print("\n".join(map(str, ans)))    


if __name__ == "__main__":
    main()

Python: # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)

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

    checked = [[0] * w for _ in range(h)]
    def dfs(x,y):
        global checked,c
        checked[y][x] = 1
        c[y][x] = 0
        for i in range(-1,2):
            for j in range(-1,2):
                if 0 <= x + i < w and 0 <= y + j < h:
                    if checked[y + j][x + i] == 0 and c[y + j][x + i] == 1:
                        dfs(x + i,y + j)

    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(j,i)
                cnt += 1

    print(cnt)

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 dy in d:
        for dx in d:
            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(10000)
def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[list(map(int,input().split())) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

Python: from queue import Queue
w, h = 0, 0

def bfs(x, y, data):
    que = Queue()
    que.put((x, y))

    while not que.empty():
        x, y = que.get()
        if data[x][y] == 0:
            continue

        data[x][y] = 0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                sx, sy = x + dx, y + dy
                if sx < 0 or sx >= h or sy < 0 or sy >= w:
                    continue
                que.put((sx, sy))
    return data


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

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

    ans = 0
    for x in range(h):
        for y in range(w):
            if data[x][y] > 0:
                ans += 1
                data = bfs(x, y, data)
    print (ans)