Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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)

Python: import sys
sys.setrecursionlimit(10000)
k = [-1,0,1]
def search(x,y,m):
    if x<0 or x>=len(m[0]) or y<0 or y>=len(m): return
    if m[y][x]==0: return
    m[y][x] = 0
    for i in k:
        for j in k:
            search(x+i,y+j,m)

while True:
    c = 0
    w,h = map(int,input().split())
    if w==0: break
    m = [list(map(int,input().split())) for i in range(h)]
    for i in range(h):
        for j in range(w):
            if m[i][j]==1:
                search(j, i, m)
                c += 1
    print(c)

Python: import sys


def dfs(M, i, j, h, w):
    M[i][j] = 0
    for dy in range(-1, 2):
        for dx in range(-1, 2):
            ny, nx = i+dy, j+dx
            if 0 <= ny < h and 0 <= nx < w and M[ny][nx] == 1:
                dfs(M, ny, nx, h, w)


def search(M, w, h):
    count = 0
    for i in range(h):
        for j in range(w):
            if M[i][j] == 1:
                dfs(M, i, j, h, w)
                count += 1
    return count


if __name__ == "__main__":
    sys.setrecursionlimit(50*50)
    ans = []
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break
        M = [[int(ipt) for ipt in input().split()] for _ in range(h)]
        ans.append(search(M, w, h))

    for a in ans:
        print(a)