sort as per the given numbers and keep the indexes later on do the multi source BFS
\nclass Solution {\npublic:\n    \n    int vis[54][54];\n    void fill_v()\n    {\n        for(int i =0;i<54;i++)\n        {\n            for(int j=0;j<54;j++)\
```\nclass Solution {\npublic:\n    \n    int vis[54][54];\n    void fill_v()\n    {\n        for(int i =0;i<54;i++)\n        {\n            for(int j=0;j<54;j++)\n            {\n                vis[i][j] = 0;\n            }\n        }\n    }\n    \n    \n    int bfs(int si, int sj, int ei, int ej,vector<vector<int>> &f)\n    {\n        queue<pair<int,pair<int,int>>> q;\n        q.push({0,{si,sj}});\n        int ans = -1;\n        \n        int n = f.size();\n        int m = f[0].size();\n        \n        while(!q.empty())\n        {\n            int level = q.front().first;\n            int i = q.front().second.first;\n            int j = q.front().second.second;\n            \n            q.pop();\n            if(vis[i][j])\n                continue;\n            \n            vis[i][j] = 1;\n            \n            \n            \n            if(i == ei && j == ej)\n            {\n                return level;\n            }\n            \n            if(i+1 < n && j<m && vis[i+1][j] == 0 && f[i+1][j])\n            {\n                q.push({level+1, {i+1, j}});\n            }\n            \n            if(i < n && j+1<m && vis[i][j+1] == 0 && f[i][j+1])\n            {\n                q.push({level+1, {i, j+1}});\n            }\n            \n            if(i-1>=0 && i-1 < n && j<m && vis[i-1][j] == 0 && f[i-1][j])\n            {\n                q.push({level+1, {i-1, j}});\n            }\n            \n            if(j-1>=0 && i< n && j-1<m && vis[i][j-1] == 0 && f[i][j-1])\n            {\n                q.push({level+1, {i, j-1}});\n            }\n        }\n        \n        return ans;\n            \n    }\n    \n    int cutOffTree(vector<vector<int>>& f) {\n        \n        \n        int n = f.size();\n        int m = f[0].size();\n        vector<pair<int,pair<int,int>>> v; \n        for(int i=0;i<n;i++)\n        {\n            for(int j=0;j<m;j++)\n            {\n                if(f[i][j]>1)\n                    v.push_back({f[i][j],{i,j}});\n            }\n            \n        }\n        \n        v.push_back({0,{0,0}});\n        int l = v.size();\n        \n        sort(v.begin(),v.end());\n        int ans = 0;\n        for(int i=0;i<l-1;i++)\n        {\n            fill_v();\n            int si,sj,ei,ej;\n            si = v[i].second.first;\n            sj = v[i].second.second;\n            \n            ei = v[i+1].second.first;\n            ej = v[i+1].second.second;\n            \n            int res = bfs( si, sj,  ei, ej,f);\n            if(res == -1)\n                return res;\n            ans+=res;\n        }\n        \n        return ans;\n    }\n};\n```
['C']