Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

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

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

Python: from collections import deque
def dfs(i,j,seen):
    q=deque()
    q.append((i,j,0))
    seen[i][j]=0
    dx=[0,1,0,-1,-1,-1,1,1]
    dy=[1,0,-1,0,1,-1,1,-1]
    while q:
        X,Y,S=q.pop()
        if S==0:
            #下に遷移していくときの処理
            #q.append((V,P,1))
            for k in range(8):
                nx=X+dx[k]
                ny=Y+dy[k]
                if not(0<=nx<h and 0<=ny<w):continue
                if seen[nx][ny]==0:continue
                seen[nx][ny]=0
                q.append((nx,ny,0))
    return 1

def main(w,h):
    grid=[list(map(int,input().split())) for i in range(h)]
    ans=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]:
                ans+=dfs(i,j,grid)
    print(ans)

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

Python: import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

def dfs(x,y):
    c[x][y]='0'
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx=dx+x
            ny=dy+y
            if c[nx][ny]=='1':dfs(nx,ny)

while 1:
    c=[]
    w,h=map(int,input().split())
    if w==h==0:exit()
    w+=2
    c=[list('@'*w)]
    for i in range(h):
        c+=[list('@'+input().replace(' ','').replace('\n','')+'@')]
    c+=[list('@'*w)]
    h+=2
    cnt=0

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

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)