C++ || Simple Bfs based solution
\nclass Solution {\npublic:\n    \n    \n    int xp[4]={-1,1,0,0};\n    int yp[4]={0,0,-1,1};\n    \n  int bfs(vector<vector<int>>& arr,int i,int j,int l,int m,
```\nclass Solution {\npublic:\n    \n    \n    int xp[4]={-1,1,0,0};\n    int yp[4]={0,0,-1,1};\n    \n  int bfs(vector<vector<int>>& arr,int i,int j,int l,int m,vector<vector<int>>&visited)\n    {\n      \n      \n      visited[i][j]=1;\n      \n      queue<pair<int,pair<int,int>>>q;\n      q.push({0,{i,j}});\n      \n      int ans=-1;\n      while(q.size())\n      {\n          int level=q.front().first;\n          int x=q.front().second.first;\n          int y=q.front().second.second;\n          q.pop();\n          \n         \n          \n          visited[x][y]=1;\n          \n          if(x==l&&y==m)\n          {\n              ans=level;\n              break;\n          }\n          \n          for(int k=0;k<4;k++)\n          {\n              int u=x+xp[k],v=y+yp[k];\n \n              if(u>=0&&v>=0&&u<arr.size()&&v<arr[0].size()&&!visited[u][v]&&arr[u][v])\n              {\n                  \n                  visited[u][v]=1;\n                  q.push({level+1,{u,v}});\n              }\n          }\n\n      }\n      return ans;        \n    }\n    \n    \n    \n    \n    int cutOffTree(vector<vector<int>>& arr) {\n           int ans=0;\n        \n        vector<pair<int,pair<int,int>>>v;\n        \n        for(int i=0;i<arr.size();i++)\n            for(int j=0;j<arr[i].size();j++)\n                if(arr[i][j]>1)\n                    v.push_back({arr[i][j],{i,j}});\n     \n        \n         v.push_back({0,{0,0}});\n         sort(v.begin(),v.end());\n\n        \n        vector<vector<int>>visited;\n        \n        for(int i=0;i<v.size()-1;i++)\n        {\n            visited.clear();\n            visited.resize(arr.size(),vector<int>(arr[0].size(),0));\n            int k=bfs(arr,v[i].second.first,v[i].second.second,v[i+1].second.first,v[i+1].second.second,visited);\n            if(k==-1)\n                 return -1;\n            else\n                ans+=k;\n        }\n\n        return ans;\n    }\n};\n```
['Breadth-First Search', 'C']