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

def landsearch(i,j):
    result=[]
    if i>0:
        if j>0:
            for x in range(-1,2):
                for y in range(-1,2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
        else:
            for x in range(-1,2):
                for y in range(2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
    elif j>0:
        for x in range(2):
            for y in range(-1,2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    else:
        for x in range(2):
            for y in range(2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    return result           


while True:
    w,h=[int(i) for i in input().split(" ")]
    if w==0 and h==0:
        sys.exit()


    atlas=[]
    for i in range(h):
        atlas.append([[int(j),0] for j in input().split(" ")])


    how_many_island=0
    for i in range(h):
        for j in range(w):
            if atlas[i][j][0]==1 and atlas[i][j][1]==0:
                how_many_island+=1
                dis_list=[[i,j]]
                while len(dis_list)!=0:
                    dis_list_cash=[]
                    for k in dis_list:
                        land=landsearch(k[0],k[1])
                        for l in land:
                            if atlas[l[0]][l[1]][1]==0:
                                atlas[l[0]][l[1]][1]=1
                                dis_list_cash.append([l[0],l[1]])
                    dis_list=dis_list_cash

    print(how_many_island)

Python: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour - {0}) > 1:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    for row in c:
        print(row)
    print(islands)
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))

Python: move = [[0, 1], [0, -1], [-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]]
from collections import deque
def main(w, h):
    c = [list(map(int, input().split())) for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x]:
                ans += 1
                q = deque()
                q.append((x, y))
                while q:
                    xx, yy = q.popleft()
                    for mx, my in move:
                        xm = xx + mx
                        ym = yy + my
                        if 0 <= xm < w and 0 <= ym < h:
                            if c[ym][xm]:
                                c[ym][xm] = 0
                                q.append((xm,ym))
    print(ans)


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

Python: import sys

# 再帰の深さの上限変更
sys.setrecursionlimit(10**4)

def DFS(v,visit):
  visit[v] = True
  for u in adj[v]:
    if visit[u] == False:
      DFS(u,visit)

while(1):
  W, H = map(int, input().split())
  if (W,H) == (0,0):# 入力終了
    sys.exit()
  
  C = []
  for _ in range(H):
    tmp = list(map(int, input().split()))
    C.append(tmp)

  # 縦横斜め8方向が島か探索し、隣接リストを作成する
  dx = [1,-1,0,0,1,-1,-1,1]
  dy = [0,0,1,-1,1,1,-1,-1]
  
  # 隣接リスト
  adj = [[] for i in range(H*W)]
  
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1:# 陸である
        # 周りが陸か探索する
        tmp = []
        for k in range(8):
          x,y = j+dx[k], i+dy[k]
          if 0 <= y < H and 0 <= x < W:
            if C[y][x] == 1:
              tmp.append(W*y + x)
        adj[W*i+j] = tmp
  # print(adj)
        
  # 深さ優先探索を行う
  # 訪問チェック
  visit = [False for i in range(H*W)]
  
  ans = 0
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1 and visit[W*i+j] == False:
        DFS(W*i+j,visit)
        ans += 1
  print(ans)