Represent the farm as a graph; the cultivated fields form the vertices, there is an edge between two fields if they are adjacent. Each patch forms a connected component in the graph.
We can use maps to store the coordinates of the cultivated fields and to convert the coordinates of the vertices into 0-based indices.
Run a DFS on the graph
For each field, the length of the fence is equal to the number of adjacent fields that are uncultivated. We can look this up in our map.
For each connected component i.e patch, The length of the fence required is the sum of the lengths of fence of the constituting vertices.
The answer is the maximum of fence required for each patch