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: import sys
sys.setrecursionlimit(10000)

def place(i, j, w):
    return i*w + j

def get_path(w, h, c):
    path = list()
    for i in range(h):
        for j in range(w):
            # place = i*w + j
            can_go = list()
            if c[i][j] != 0:
                if i != 0 and c[i-1][j] != 0:
                    can_go.append( place(i-1, j, w) )
                
                if i != h-1 and c[i+1][j] != 0:
                    can_go.append( place(i+1, j, w) )

                if j != 0 and c[i][j-1] != 0:
                    can_go.append( place(i, j-1, w) )
                
                if j != w-1 and c[i][j+1] != 0:
                    can_go.append( place(i, j+1, w) )
                
                if i != 0 and j!= 0 and c[i-1][j-1] != 0:
                    can_go.append( place(i-1, j-1, w) )
                
                if i != 0 and j != w-1 and c[i-1][j+1] != 0:
                    can_go.append( place(i-1, j+1, w) )
                
                if i!= h-1 and j != 0 and c[i+1][j-1] != 0:
                    can_go.append( place(i+1, j-1, w) )
                
                if i!= h-1 and j != w-1 and c[i+1][j+1] != 0:
                    can_go.append( place(i+1, j+1, w) )
                
            path.append(can_go)
    
    return path

def dfs(p, path, visit, w, h, c):
    visit[p] = 1 # 来訪時刻の記録

    for nxt in path[p]: #繋がってる点の内
        if visit[nxt] == 0: # 未探索の場合には
            dfs(nxt, path, visit, w, h, c) # 先に進む
    
    return

# def print_path(i, j, path, w, h):
#     arr = path[i*w + j]
#     print("print path start")
#     for x in arr:
#         print(x//w, x%w)
#     print("print path fin")

def solve(w, h, c):
    path = get_path(w, h, c)
    visit = [0]*(w*h)

    # print_path(3, 18, path, w, h)
    ans = 0
    for place in range(w*h):
        i = place // w
        j = place % w
        if c[i][j] != 0 and visit[place] == 0:
            dfs(place, path, visit, w, h, c)
            # print(i, j)
            ans += 1
    
    print(ans)
    return 

w, h = map(int, input().split())
while w != 0 and h != 0:
    c = [list(map(int, input().split())) for _ in range(h)]
    solve(w, h, c)
    w, h = map(int, input().split())

Python: from collections import deque

while True:
    w,h=map(int,input().split())
    if (w,h)==(0,0):
        break
    p=[]
    p.append([False for j in range(w+2)])
    for i in range(1,h+1):
        p.append([False]+[cij==1 for cij in list(map(int,input().split()))]+[False])
    p.append([False for j in range(w+2)])

    r=[[False for j in range(w+2)] for i in range(h+2)]
    ans=0
    for i in range(1,h+1):
        for j in range(1,w+1):
            if p[i][j] and not r[i][j]:
                q=deque()
                q.append((i,j));r[i][j]=True
                while q:
                    u=q.popleft()
                    ui,uj=u
                    for v in [(vi,vj) for vi in [ui-1,ui,ui+1] for vj in [uj-1,uj,uj+1]]:
                        vi,vj=v
                        if p[vi][vj] and not r[vi][vj]:
                            q.append(v);r[vi][vj]=True
                ans+=1
    print(ans)

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.getrecursionlimit(10000)
    main()

Python: import sys
def f(n):
  print>>sys.stderr, n
  f(n+1)
f(1)

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.setrecursionlimit(100000)
    main()