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: # coding: utf-8
import sys
sys.setrecursionlimit(2147483647)
def check(p):
    global field
    if(field[p[1]][p[0]]=='1'):
        field[p[1]][p[0]]='0'
    if(0<=p[0]+1<w and field[p[1]][p[0]+1]=='1'):
        check((p[0]+1,p[1]))
    if(0<=p[0]-1<w and field[p[1]][p[0]-1]=='1'):
        check((p[0]-1,p[1]))
    if(0<=p[1]+1<h and field[p[1]+1][p[0]]=='1'):
        check((p[0],p[1]+1))
    if(0<=p[1]-1<h and field[p[1]-1][p[0]]=='1'):
        check((p[0],p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]+1]=='1'):
        check((p[0]+1,p[1]-1))
    if(0<=p[0]-1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]-1]=='1'):
        check((p[0]-1,p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]+1]=='1'):
        check((p[0]+1,p[1]+1))
    if(0<=p[0]-1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]-1]=='1'):
        check((p[0]-1,p[1]+1))
     
while 1:
    ans=0
    w,h=map(int,input().split())
    if w==0:
        break
    field=[]
    for i in range(h):
        s=''.join(input().split())
        field.append(list(s))
    for i in range(h):
        for j in range(w):
            if field[i][j]=='1':
                check((j,i))
                ans+=1
    print(ans)

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


def dfs(h, w):
    board[h][w] = 0
    for dh in range(-1, 2):
        for dw in range(-1, 2):
            nh = h + dh
            nw = w + dw
            if 0 <= nh < H and 0 <= nw < W and board[nh][nw] == 1:
                dfs(nh, nw)


while True:
    W, H = map(int, input().split())
    if H == 0 and W == 0:
        break
    board = [list(map(int, input().split())) for _ in range(H)]
    ans = 0
    for h in range(H):
        for w in range(W):
            if board[h][w]:
                dfs(h, w)
                ans += 1
    print(ans)

Python: 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
from itertools import product as pro
sys.setrecursionlimit(10**7)

while True:
    w,h=map(int,input().split())
    if w==0 and h==0:
        sys.exit()
    
    G=[]
    for i in range(h):
        G.append(list(map(int,input().split())))

    #隣接する島
    def sur(X):
        x=X[0]
        y=X[1]
        axis_x=[x+1,x,x-1]
        axis_y=[y+1,y,y-1]
        preE=list(pro(axis_x,axis_y))
        preE.remove((x,y))
        E=[]
        for e in preE:
            if 0<=e[0]<=h-1 and 0<=e[1]<=w-1:
                if G[e[0]][e[1]]==1:
                    E.append(e)
        return E

    check=[[False]*w for i in range(h)]

    #checkの陸埋めをしても良い
    def dfs(v):
        check[v[0]][v[1]]=True
        for next in sur(v):
            if check[next[0]][next[1]]:
                continue
            dfs(next)

    ans=0
    for i in range(h):
        for j in range(w):
            if not check[i][j] and G[i][j]==1:
                ans+=1
                dfs((i,j))
    
    print(ans)

Python: import sys
sys.setrecursionlimit(10**6)

w=0;h=0;
def dfs(x,y,Map):
    #ending condition
    if not (0 <= x and x < h and 0 <= y and y < w):
        return
    if Map[x][y] == 0:
        return
    
    #operation
    Map[x][y] = 0

    #moving deep
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            dfs(x+i,y+j,Map)

def start_cnt():
    global w,h
    w,h = map(int,input().split())
    if w == 0 and h == 0:exit();
    cnt = 0
    Map = []
    for i in range(h):
            Map.append(list(map(int,input().split())))
    for i in range(h):
        for j in range(w):
            if Map[i][j] == 1:
                dfs(i,j,Map)
                cnt += 1
    print(cnt)
while True:
    start_cnt()

Python: import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
def dfs(a,v):
    global st,per
    if v in st: st.remove(v)
    else: return
    for tom in per:
        if a[v[0]+tom[0]][v[1]+tom[1]]==1: dfs(a,(v[0]+tom[0],v[1]+tom[1]))
while True:
    c,r=map(int,input().split())
    if c==0 and r==0 : exit(0)
    a=[[0]*(c+2)]
    for j in range(r):
        a.append([0]+[int(i) for i in input().split()]+[0])
    a.append([0]*(c+2))
    st=set()
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j]==1:
                st.add((i,j))
    per=[[1,1],[1,0],[0,1],[-1,1],[1,-1],[-1,-1],[-1,0],[0,-1]]
    ans=0
    while st:
        ans+=1
        for i in st:
            dfs(a,i)
            break
    print(ans)