#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> II;
const int N = 1e5 + 5, logN = 20;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
int n, m, vis[N][55], minx, maxx, comp[N][55], nComp;
char a[N][55];
vector<II> vt;
inline bool inside(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= m;
}
void dfs(int x, int y){
vis[x][y] = true;
vt.push_back(II(x, y));
comp[x][y] = nComp;
minx = min(minx, x);
maxx = max(maxx, x);
for(int nx = x - 1; nx <= x + 1; nx ++){
for(int ny = y - 1; ny <= y + 1; ny ++){
if(inside(nx, ny) && !vis[nx][ny] && a[nx][ny] == '0') dfs(nx, ny);
}
}
}
int main(){
//freopen("datpat.inp", "r", stdin);
//freopen("datpat.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++) cin >> a[i][j];
}
ll ans = 0;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
if(a[i][j] == '1'){
ans += i * 1ll * (n - i + 1);
if(a[i - 1][j] == '1') ans -= (i - 1) * 1ll * (n - i + 1);
if(a[i][j - 1] == '1') ans -= i * 1ll * (n - i + 1);
if(a[i - 1][j] == a[i][j - 1] && a[i - 1][j] == '1' && a[i - 1][j - 1] == '1') ans += (i - 1) * 1ll * (n - i + 1);
}
}
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
if(!vis[i][j] && a[i][j] == '0'){
minx = INF;
maxx = -INF;
nComp ++;
vt.clear();
dfs(i, j);
bool check = true;
for(auto xy : vt){
int x = xy.first, y = xy.second;
for(int nx = x - 1; nx <= x + 1; nx ++){
for(int ny = y - 1; ny <= y + 1; ny ++) check &= (comp[nx][ny] == nComp || a[nx][ny] == '1');
}
}
if(check) ans += (minx - 1) * 1ll * (n - maxx);
}
}
}
cout << ans << endl;
return 0;
}
/*
5 7
0100010
0111110
0101001
1111011
0100100
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
3928 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
856 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3668 KB |
Output is correct |
2 |
Correct |
188 ms |
102804 KB |
Output is correct |
3 |
Correct |
107 ms |
50252 KB |
Output is correct |
4 |
Correct |
104 ms |
109936 KB |
Output is correct |
5 |
Correct |
47 ms |
15912 KB |
Output is correct |
6 |
Correct |
137 ms |
50408 KB |
Output is correct |
7 |
Correct |
84 ms |
50196 KB |
Output is correct |
8 |
Correct |
77 ms |
40328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
3928 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
856 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
4 ms |
852 KB |
Output is correct |
14 |
Correct |
14 ms |
3668 KB |
Output is correct |
15 |
Correct |
188 ms |
102804 KB |
Output is correct |
16 |
Correct |
107 ms |
50252 KB |
Output is correct |
17 |
Correct |
104 ms |
109936 KB |
Output is correct |
18 |
Correct |
47 ms |
15912 KB |
Output is correct |
19 |
Correct |
137 ms |
50408 KB |
Output is correct |
20 |
Correct |
84 ms |
50196 KB |
Output is correct |
21 |
Correct |
77 ms |
40328 KB |
Output is correct |
22 |
Correct |
352 ms |
170524 KB |
Output is correct |
23 |
Correct |
295 ms |
53756 KB |
Output is correct |
24 |
Correct |
214 ms |
53704 KB |
Output is correct |
25 |
Correct |
534 ms |
291324 KB |
Output is correct |
26 |
Correct |
402 ms |
64052 KB |
Output is correct |
27 |
Correct |
323 ms |
56564 KB |
Output is correct |
28 |
Correct |
274 ms |
52844 KB |
Output is correct |
29 |
Correct |
513 ms |
224976 KB |
Output is correct |
30 |
Correct |
422 ms |
96152 KB |
Output is correct |
31 |
Correct |
431 ms |
105492 KB |
Output is correct |
32 |
Correct |
227 ms |
53872 KB |
Output is correct |
33 |
Correct |
227 ms |
48384 KB |
Output is correct |
34 |
Correct |
291 ms |
49716 KB |
Output is correct |