#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(x) (x).begin(),(x).end() #define rep(i,m,n) for(int i = m;i < n;++i) #define pb push_back #define fore(i,a) for(auto &i:a) #define rrep(i,m,n) for(int i = m;i >= n;--i) #define INF INT_MAX/2 using namespace std; using ll = long long; using R = double; using Data = pair>; const ll MOD = 1e9 + 7; const ll inf = 1LL << 50; struct edge { ll from; ll to; ll cost; }; vector>mp(3030,vector(3030)); int used[3030][3030]; int h, w; void bfs(int x, int y) { used[y][x] = 1; queue>q; q.push({x,y}); while (!q.empty()) { auto now = q.front(); q.pop(); int xnow = now.first; int ynow = now.second; int dx[4] = { -1,0,1,0 }; int dy[4] = { 0,-1,0,1 }; rep(i, 0, 4) { int _x = xnow + dx[i]; int _y = ynow + dy[i]; if (0 <= _x && _x <= w - 1 && 0 <= _y && _y <= h - 1) { if (used[_y][_x] == 0 && mp[_y][_x] == '1') { q.push({ _x,_y }); used[_y][_x] = 1; } } } } return; } int main() { cin >> h >> w; rep(i, 0, h)rep(j,0,w)cin >> mp[i][j]; int ans = 0; rep(i, 0, h)rep(j,0,w){ if (mp[i][j] == '1' && used[i][j] == 0) { bfs(j,i); ans++; } } cout << ans << endl; return 0; }