How can i use a script to automatic place invisible walls around the terrain edges ?

For now i did it on my own. Created 4 empty gameobjects added box collider to each one of them and then dragged them with the mouse to the terrain edges.
But they are not in the exactly places i mean not exactly touching the edges of the terrain.

Is there a way to calculate with a script and find the edges of the terrain positions and place the walls automatic to fit each edge ?

You could either use the V key to align the GameObjects to the terrain in the editor mode or try instatiating new GameObjects based on the terrain position and length in runtime.
I’ve created a simple script that will do it, the terrain doesn’t need to have same width and length to make this work:

public class TerrainBorderGenerator : MonoBehaviour {
    //Attach this script to the terrain containing GameObject
	void Start () {
        //Get the terrain position and size
        Vector3 terrainPosition = transform.position;
        Vector3 terrainSize = GetComponent<Terrain>().terrainData.size;
        //Call the insantiating function
        InstantiateEdgeWidthBorders(terrainPosition, terrainSize);
        InstantiateEdgeLengthBorders(terrainPosition, terrainSize);
    }
    //Since our borders size will be the same, we divide the borders in two cases: Width and Length
    private void InstantiateEdgeWidthBorders(Vector3 terrainPosition, Vector3 terrainSize){
        //We calculate the width offset for our borders 
        //Since our left border will be in the same position as the terrain X position, we only need to calculate the right border offset 
        float zOffset = terrainSize.z;
        //We also need to calculate the X offset of the terrain (wich is the half of it size) to center the borders to the terrain
        float xOffset = terrainSize.x / 2;
        //Since Width borders share same scale... 
        for (int i = 0; i < 2; i++){
            GameObject border = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //As we don't care of the length, we assign 1 in the Z scale
            border.transform.localScale = new Vector3(terrainSize.x,terrainSize.y,1);
            //Now we need to calculate the position of our borders based on our terrain position , we will asume that i = 0 is the left border and i = 1 the right one
            if (i == 0)
                border.transform.position = new Vector3(terrainPosition.x + xOffset, terrainPosition.y, terrainPosition.z);
            else
                border.transform.position = new Vector3(terrainPosition.x + xOffset, terrainPosition.y, terrainPosition.z + zOffset);
            //We disable our mesh renderer
            DisableMeshRenderer(border);
            //For clean scene purposes, we will make our borders childs of the terrain and assign them a clear name
            border.transform.name = "Width Border " + (i + 1);
            border.transform.parent = transform;
        }
    } 
    //Same procedure goes for the length borders
    private void InstantiateEdgeLengthBorders(Vector3 terrainPosition, Vector3 terrainSize){
        //Since we are looking for length, we need to change these offsets
        float xOffset = terrainSize.x;
        float zOffset = terrainSize.z / 2;
        for (int i = 0; i < 2; i++)
        {
            GameObject border = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //We don't care about the width, so we assign 1 into the X axis
            border.transform.localScale = new Vector3(1, terrainSize.y, terrainSize.z);
            if (i == 0)
                border.transform.position = new Vector3(terrainPosition.x, terrainPosition.y, terrainPosition.z + zOffset);
            else
                border.transform.position = new Vector3(terrainPosition.x + xOffset, terrainPosition.y, terrainPosition.z + zOffset);
            DisableMeshRenderer(border);
            border.transform.name = "Length Border " + (i + 1);
            border.transform.parent = transform;
        }
    }
    //We don't want our borders to be seen, do we?
    private void DisableMeshRenderer(GameObject border){
        border.GetComponent<MeshRenderer>().enabled = false;
    }
}