1 include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<string>
9 #include<cmath>
10 #define N 1000005
11 using namespace std;
12 int n,m,zz,bh[1005][1004];
13 struct no{
14 int high;
15 int x,y;
16 }node[N],city[N];
17 bool fw[N];
18 int h[1005][1005];
19 int fa[N],zz2;
20 int px(no x,no y){
21 return x.high<y.high;
22 }
23 int find(int x){
24 if(fa[x]==x)
25 return x;
26 else
27 return fa[x]=find(fa[x]);
28 }
29 void hb(int x,int y){
30 int a=find(x);
31 int b=find(y);
32 fa[b]=a;
33 fw[a]|=fw[b];
34 }
35 void check(no a){
36 int x=a.x,y=a.y;
37 if(x+1<=n&&h[x+1][y]<=h[x][y])
38 {
39 hb(bh[x+1][y],bh[x][y]);
40 }
41 if(x-1>=1&&h[x-1][y]<=h[x][y])
42 {
43 hb(bh[x-1][y],bh[x][y]);
44 }
45 if(y+1<=m&&h[x][y+1]<=h[x][y])
46 {
47 hb(bh[x][y+1],bh[x][y]);
48 }
49 if(y-1>=1&&h[x][y-1]<=h[x][y])
50 {
51 hb(bh[x][y-1],bh[x][y]);
52 }
53 }
54
55 int main(){
56 scanf("%d%d",&n,&m);
57 for(int i=1;i<=n;i++)
58 {
59 for(int j=1;j<=m;j++)
60 {
61 int x;
62 scanf("%d",&x);
63 if(x>0)
64 {
65 zz++;
66 city[zz].x=i;
67 city[zz].y=j;
68 city[zz].high=x;
69 }
70 zz2++;
71 node[zz2].x=i;
72 node[zz2].y=j;
73 node[zz2].high=abs(x);
74 h[i][j]=abs(x);
75 bh[i][j]=zz2;
76 }
77 }
78 sort(node+1,node+zz2+1,px);
79 sort(city+1,city+zz+1,px);
80 for(int i=1;i<=zz2;i++)
81 {
82 fa[i]=i;
83 }
84 int ans=0;
85 for(int j=1,i=1;i<=zz;i++)
86 {
87 for(;j<=zz2&&node[j].high<=city[i].high;j++)
88 {
89 check(node[j]);
90 }
91 if(!fw[find(bh[city[i].x][city[i].y])])
92 {
93 ans++;
94 fw[fa[bh[city[i].x][city[i].y]]]=1;
95 }
96 }
97 printf("%d\n",ans);
98 //while(1);
99 return 0;
100 }
101