fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. using ll=long long;
  5. #define nl '\n'
  6. #define fn ""
  7. #define F first
  8. #define S second
  9. #define rs resize
  10. #define kt size
  11. int dx[]={-1,1,0,0};
  12. int dy[]={0,0,-1,1};
  13. int n,m;
  14. vector<vector<int>> a;
  15. bool check (int i,int j) {
  16. int cnt=0;
  17. for (int k=0;k<4;k++) {
  18. int x=i+dx[k];
  19. int y=j+dy[k];
  20. if (x<1 || y<1 || x>n || y>m) continue;
  21. if (a[x][y]==0) cnt++;
  22. }
  23. return cnt>=2;
  24. }
  25. bool checkfull() {
  26. for (int i=1;i<=n;i++) {
  27. for (int j=1;j<=m;j++) {
  28. if (a[i][j]==1) return true;
  29. }
  30. }
  31. return false;
  32. }
  33. int main () {
  34. ios_base::sync_with_stdio(false);
  35. cin.tie (nullptr);
  36. if (FILE *f=fopen(fn".INP","r")) {
  37. fclose(f);
  38. freopen (fn".INP","r",stdin);
  39. freopen (fn".OUT","w",stdout);
  40. }
  41. cin >> n >> m;
  42. a.resize(n+1,vector <int> (m+1));
  43. for (int i=1;i<=n;i++) {
  44. for (int j=1;j<=m;j++) cin >> a[i][j];
  45. }
  46. int ans=0;
  47. while (checkfull()) {
  48. vector<pair<int,int>> v;
  49. for (int i=1;i<=n;i++) {
  50. for (int j=1;j<=m;j++) {
  51. if (check(i,j)) v.push_back({i,j});
  52. }
  53. }
  54. if (v.empty()) break;
  55. for (auto p:v) a[p.F][p.S]=0;
  56. ans++;
  57. }
  58. cout << ans;
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Standard output is empty