#include "bits/stdc++.h" using namespace std; typedef long long int ll; typedef pair pi; typedef pair, ll > pii; vector vec; vector > vec2; ll MOD = 1000000007; ll INF = 1145141919; ll ans = 0; ll N, M; vector > G; ll dx[4] = {1, -1, 0, 0}; ll dy[4] = {0, 0, 1,-1}; void bfs(ll s_y, ll s_x){ //準備 vector > dist(N, vector(M, -1)); queue que; //初期化 dist[s_y][s_x] = 0; que.push(make_pair(s_y, s_x)); while(!que.empty()){ ll from_y = que.front().first; ll from_x = que.front().second; G[from_y][from_x] = -1; que.pop(); //一度通ったところは for(ll i = 0; i < 4; i++){ if(from_y + dy[i] >= 0 && from_y + dy[i] < N && from_x + dx[i] >= 0 && from_x + dx[i] < M){ ll to_y = from_y + dy[i]; ll to_x = from_x + dx[i]; //池ではない時 if(G[to_y][to_x] == -1 || dist[to_y][to_x] != -1) continue; dist[to_y][to_x] = dist[from_y][from_x] + 1; que.push(make_pair(to_y, to_x)); } } } ans++; } int main(){ cin >> N >> M; G.assign(N, vector()); //初期化 for(ll i = 0; i < N; i++){ for(ll j = 0; j < M; j++){ ll t; cin >> t; if(t == 0){ G[i].push_back(-1); }else{ G[i].push_back(INF); } } } for(ll i = 0; i < N; i++){ for(ll j = 0; j < M; j++){ if(G[i][j] == -1) continue; bfs(i, j); } } cout << ans << endl; }