#include #include #include #include #include #include using namespace std; #define REP(i,n) for(int i=0;i<(n);++i) #define REPS(i,s,t) for(int i=(s);i<(t);++i) #define PER(i,n) for(int i=0;i>(n);--i) #define PERS(i,s,t) for(int i=(s);i>(t);--i) #define INF 2000000007 #define MINF -200000007 #define MOD 17 // 2^20 #define MAX 200005 typedef unsigned int uint; typedef unsigned long long int ull; typedef long long int ll; template istream& operator >> (istream& is, vector& vec){ for(T& x: vec) is >> x; //for(int i=0; i> x[i]; とかでもいいです。 return is; } //int dp[MAX]; int H,W; int cnt; int dis; void check(vector > &A,int y,int x){ if(A[y][x]==0)return; int n = (y!=0) ? A[y-1][x] : 0; int w = (x!=0) ? A[y][x-1] : 0; if(n!=0&&w!=0){ A[y][x] = min(n,w); if(n!=w) ++dis; } else if(n==0&&w==0) A[y][x] = ++cnt; else A[y][x] = n+w; } int main(){ cin>>H>>W; vector > A(H,vector(W,false)); cin>>A; REP(i,H) REP(j,W) check(A,i,j); cout << (cnt-dis) << endl; return 0; }