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: def solve():
    from sys import stdin, setrecursionlimit
    setrecursionlimit(4000)
    f_i = stdin
    
    def dfs(pos):
        area_map[pos] = '0'
        for mv in move:
            next_pos = pos + mv
            if area_map[next_pos] == '1':
                dfs(next_pos)
    
    while True:
        w, h = map(int, f_i.readline().split())
        if w == 0:
            break
        
        area_map = ['0'] * (w + 2)
        for i in range(h):
            area_map += ['0'] + f_i.readline().split() + ['0']
        area_map += ['0'] * (w + 2)
        
        move = (1, -1, w + 1, w + 2, w + 3, -w - 3, -w - 2, -w - 1)
        
        ans = 0
        for i in range((w + 2) * (h + 2)):
            if area_map[i] == '1':
                dfs(i)
                ans += 1
        
        print(ans)

solve()

Python: import sys

dx = [0, 1, 1, 1, 0, -1, -1, -1]
dy = [1, 1, 0, -1, -1, -1, 0, 1]
w = 0
h = 0


def main():
    global w, h

    while True:
        count = 0
        w, h = map(int, input().split())
        if w is 0 and h is 0:
            break
        field = []
        [field.append(list(map(int, input().split()))) for _ in range(h)]

        for y in range(h):
            for x in range(w):
                if field[y][x] == 1:
                    dfs(field, x, y)
                    count += 1

        print(count)


def dfs(field, x, y):
    field[y][x] = 0

    for dx_, dy_ in zip(dx, dy):
        next_x = x + dx_
        next_y = y + dy_
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h or field[next_y][next_x] == 0:
            continue
        dfs(field, next_x, next_y)


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

Python: import sys

sys.setrecursionlimit(1000000)

D = [(1,0), (0,1), (-1,0), (0,-1), (-1,-1),(1,-1),(-1,1),(1,1) ] # 下、右、上、左、斜め

def dfs(x,y):
  # Validation
  if not(0<=x<H and 0<=y<W) : return

  # 探索済みか、探索可能かを確認する
  if f[x][y]!=0 or G[x][y] == 0: return

  # Validation完了、探索範囲内、探索可能、ゴール以外＝＞visitフラグを立てる
  f[x][y]=1
 
  # 次の場所に向かう
  for dx,dy in D:
     nx=x+dx
     ny=y+dy
     dfs(nx,ny)
  return

def main():

    cnt=0
    for i in range(H):
        for j in range(W):
            if f[i][j]==0 and G[i][j]: 
                dfs(i, j)
                cnt+=1
    print(cnt)

while True:
    W,H=map(int,input().split())
    if W==0 and H==0: break
    G=[list(map(int,input().split())) for _ in range(H)]
    f=[[0]*W for _ in range(H)]

    main()

Python: import sys
sys.setrecursionlimit(20000)
w,h=list(map(int,input().split()))
def simaserach(i,j):
    if color[i][j]=="w":
        color[i][j]="g"
        if i>=1:
            if sima[i-1][j]==1:
                simaserach(i-1,j)
            if j>=1:
                if sima[i-1][j-1]==1:
                    simaserach(i-1,j-1)
            if j<=w-2:
                if sima[i-1][j+1]==1:
                    simaserach(i-1,j+1)
        if j>=1:
            if sima[i][j-1]==1:
                simaserach(i,j-1)
        if j<=w-2:
            if sima[i][j+1]==1:
                simaserach(i,j+1)
        if i<=h-2:
            if j>=1:
                if sima[i+1][j-1]==1:
                    simaserach(i+1,j-1)
            if sima[i+1][j]==1:
                simaserach(i+1,j)
            if j<=w-2:
                if sima[i+1][j+1]:
                    simaserach(i+1,j+1)

while(w!=0):
    count=0
    color = [["w" for i in range(w)] for j in range(h)]
    sima=[[]for i in range(h)]
    for i in range(h):
        a=list(map(int,input().split()))
        sima[i]=a
    for i in range(h):
        for j in range(w):
            if sima[i][j]==1 and color[i][j]=="w":
                simaserach(i,j)
                count+=1
    print(count)
    w, h = list(map(int, input().split()))

Python: import sys
sys.setrecursionlimit(10**6)
from itertools import product
while True:
  w,h = map(int,input().split())
  if w == h == 0: exit()
  islands = []
  for _ in range(h):
    islands.append(list(map(int,input().split())))

  seen = [ [False]*w for _ in range(h) ]
  def dfs(row,col):
    seen[row][col] = True

    for x,y in product(range(-1,2), repeat=2):
      if x==0 and y==0: continue
      nrow, ncol = row+x, col+y
      if nrow>=h or nrow<0 or ncol>=w or ncol<0: continue
      if seen[nrow][ncol]: continue

      if islands[nrow][ncol] == 1:
        dfs(nrow, ncol)

  res = 0
  for i in range(h):
    for j in range(w):
      if islands[i][j] == 1 and not seen[i][j]:
        dfs(i,j)
        res += 1

  print(res)

Python: import sys
sys.setrecursionlimit(2500)
 
def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
            erase(nx, ny)
 
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [sys.stdin.readline().split() for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)